1

[LIVE]

\U0001F34C\U0002008A on cout  : 
\U0001F34C\U0002008A on wcout : 
                      wcsnlen : 4
1 :     0xd83c
2 :     0xdf4c
3 :     0xd840
4 :     0xdc8a
    wcsrtombs to char on cout : 

Even though UCRT makes MSVC more compliant with the standard.

Each character above BMP on Windows is stored as a UTF-16 surrogate pairs. It looks like standard std::wcout on Windows is implemented in the way it can't display any of these characters.

Other than convert back from wchar_t to multibyte char (UTF-8), can we make Windows std::wcout display these kind of characters right away?

sandthorn
  • 2,770
  • 1
  • 15
  • 59
  • 1
    If you want to output `UTF-16` you need to set the terminal to `UTF-16` so it understands what you are outputting. – Galik Sep 05 '22 at 10:18
  • @Galik Could you remind us how to do that please, I can never remember. Thx. But, OP, surrogate pairs are nothing special in this regard, although not all code points are displayable in all fonts. – Paul Sanders Sep 05 '22 at 10:23
  • @PaulSanders How you do that will depend on the terminal you are using. On the terminal I use most, I have a drop-down menu to select which encoding I want it to display. – Galik Sep 05 '22 at 10:43
  • 2
    Have a read of https://stackoverflow.com/questions/71328697/using-utf-16-for-i-o-with-visual-studio-instead-of-code-pages – Richard Critten Sep 05 '22 at 11:28
  • @RichardCritten Thanks. Well, unfortunately `WriteConsoleW` seems to be only API to work with `wchar_t` string directly, not the standard `std::wcout`. – sandthorn Sep 05 '22 at 15:14

0 Answers0