The key is that you have to make the type of the datum such that the compiler picks the overloaded operator<<()
for cout
that results in numeric output. You can set the std::hex
iomanip flag all you want, the type of a char
or wchar_t
will always pick the operator<<()
that outputs a readable character, not the value of it. To get the compiler to pick the operator<<()
that will print a value, you have to cast the character as a numeric type. In my example below, I cast it to a uint32_t
because 32 bits is sufficient to represent any unicode character. I could cast it to an int
on my system since on my system, ints are 32 bits, but to ensure that your code is portable even to tiny embedded systems where int
is 16 bits and you hardly have enough memory to process ASCII let alone unicode, you should cast it to a type that's guaranteed to be 32 bits or more. long
would also be sufficient.
#include <iostream>
#include <iomanip>
#include <stdint.h>
int main()
{
cout << hex << '\u0068' << dec << endl;
wchar_t c;
std::wcout << "Type a character... " << std::endl;
std::wcin >> c;
std::wcout << "Your character was '" << c << "' (unicode "
<< std::hex << std::setw(4) << static_cast<uint32_t>(c) << ")\n";
}