domonica
domonica

Reputation: 566

Output a Unicode Number in C++ (not the character)

There's plenty of information out there on how to output unicode Characters in C++. I'm able to do this with the below code. This outputs a lower case 'h'.

cout << '\u0068' << endl;

is it possible to go the other way. That is, input a 'h' and have cout display the 0068 (unicode number) in a console?

I have googled this to death and found nothing. I don't want to build a lookup table/switch statement etc. I though there may be simple way of converting unicode characters into their unicode numbers using something as simple as above. Any clues.

Upvotes: 2

Views: 1643

Answers (1)

phonetagger
phonetagger

Reputation: 7873

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";
}

Upvotes: 3

Related Questions