简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM