Properly print utf8 characters in windows console

前端 未结 7 563
慢半拍i
慢半拍i 2020-11-29 04:34

This is the way I try to do it:

#include 
#include 
using namespace std;

int main() {
  SetConsoleOutputCP(CP_UTF8);
   //ge         


        
7条回答
  •  广开言路
    2020-11-29 05:14

    I solved the problem in the following way:

    Lucida Console doesn't seem to support umlauts, so changing the console font to Consolas, for example, works.

    #include 
    #include 
    
    int main()
    {
        SetConsoleOutputCP(CP_UTF8);
    
        // I'm using Visual Studio, so encoding the source file in UTF-8 won't work
        const char* message = "a" "\xC3\xA4" "bcdefghijklmno" "\xC3\xB6" "pqrs" "\xC3\x9F" "tu" "\xC3\xBC" "vwxyz";
    
        // Note the capital S in the first argument, when used with wprintf it
        // specifies a single-byte or multi-byte character string (at least on
        // Visual C, not sure about the C library MinGW is using)
        wprintf(L"%S", message);
    }
    

    EDIT: fixed stupid typos and the decoding of the string literal, sorry about those.

提交回复
热议问题