Search code examples
c++unicodec++11utf-32

Print char32_t to console


How can I print (cout / wcout / ...) char32_t to console in C++11?

The following code prints hex values:

u32string s2 = U"Добрый день";
for(auto x:s2){
    wcout<<(char32_t)x<<endl;
}

Solution

  • First, I don't think wcout is supposed to print as characters anything but char and wchar_t. char32_t is neither.

    Here's a sample program that prints individual wchar_t's:

    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      wcout << (wchar_t)0x41 << endl;
      return 0;
    }
    

    Output (ideone):

    A
    

    Currently, it's impossible to get consistent Unicode output in the console even in major OSes. Simplistic Unicode text output via cout, wcout, printf(), wprintf() and the like won't work on Windows without major hacks. The problem of getting readable Unicode text in the Windows console is in having and being able to select proper Unicode fonts. Windows' console is quite broken in this respect. See this answer of mine and follow the link(s) in it.