c++windowsvisual-studiounicodechinese-locale

How do I use system("chcp 936") in my dialog based project?


The code below is supposed to convert a wstring "!" to a string and output it,

    setlocale(LC_ALL, "Chinese_China.936");
    //system("chcp 936"); 
    
    std::wstring ws = L"!";
    string as((ws.length()) * sizeof(wchar_t), '-');
    auto rs = wcstombs((char*)as.c_str(), ws.c_str(), as.length());
    as.resize(rs);
    cout << rs << ":" << as << endl;

If you run it without system("chcp 936");, the converted string is "£¡" rather than "!". If with system("chcp 936");, the result is correct in a console project.

enter image description here

But on my Dialog based project, system("chcp 936")is useless, even if it's workable, I can't use it, because it would popup a console.

enter image description here

PS: the IDE is Visual Studio 2019, and my source code is stored as in UTF-8 with signature. My operation system language is English and language for non-unicode programs is English (United States).

enter image description here

Edit: it's interesting, even with "en-US" locale, "!" can be converted to an ASCII "!".

enter image description here

But I don't get where "£¡" I got in the dialog based project.


Solution

  • There are two distinct points to considere with locales:

    The first point depends on the language and optionaly libraries that you use in your program (here the C++ language and Standard Library)

    The second point depends on the console application and underlying system. Windows console uses chcp, and you will find in that other post how you can configure xterm in a Unix-like system.