How to print UTF-8 strings to std::cout on Windows?

后端 未结 7 1235
南旧
南旧 2020-12-08 07:11

I\'m writing a cross-platform application in C++. All strings are UTF-8-encoded internally. Consider the following simplified code:

#include 
#         


        
7条回答
  •  被撕碎了的回忆
    2020-12-08 07:57

    Forget everything you know about the Windows console and its Unicode/UTF-8 support (or rather lack of support). This is 2020 and it's a new world. This is not a direct answer to the question above, but rather an alternative that makes much more sense now, a new way that was not possible before.

    Everybody's right, the root problem is the Windows console. But there's a new player in town, and it's Windows Terminal. Install and launch Windows Terminal. Use this program:

    #include 
    #include 
    
    int main()
    {
        SetConsoleOutputCP(CP_UTF8); 
        // or have your user set the console codepage: `chcp 65001`
        
        std::cout << "\"u\" with two dots on top: \xc3\xbc\n";
        std::cout << "chinese glyph for \"world\": \xe5\x80\xbc\n";
        std::cout << "smiling emoji: \xf0\x9f\x98\x80\n";
        return 0;
    }
    

    This program sends UTF-8 through a plain cout.

    The output:

    Unicode output in Windows Terminal

    The command chcp 65001 or SetConsoleOutputCP(CP_UTF8) is required for a cmd tab in Windows Terminal, but it looks like it is not in a Powershell tab. Maybe Powershell is UTF-8 by default?

    Rooting out the core issue, cmd, is now the best option in my opinion. Spread the word.

提交回复
热议问题