Why does C style cast work but reinterpret_cast doesn't?

Viewed 1322

So I have a two char array

unsigned char v[2];

I want to show the value of v[0] as a number from 0 to 255 but

cout << v[0] << endl; //prints some garbage

cout << (void*)v[0] << endl; //prints the right thing but in hex

So I tried

cout << (int)v[0] << endl;

or

printf("%d\n", v[0]);

This shows exactly what I wanted but I don't like it at all. Also what I don't understand at all is why this doesn't work:

cout << reinterpret_cast<int>(v[0]) << endl; //compiler error
6 Answers
Related