0

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.

Zhang
  • 3,030
  • 2
  • 14
  • 31
  • 2
    Programs should read and accomodate the user's locale. They should not force their own locale on the user. – stark Jan 03 '22 at 15:46
  • https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question/285557#285557 – Marek R Jan 03 '22 at 16:19
  • this should be helpful to understand what is the problem: https://stackoverflow.com/a/67819605/1387438 – Marek R Jan 03 '22 at 16:21

2 Answers2

0

There are two distinct points to considere with locales:

  • you must tell the program what charset should be used when converting unicode characters to plain bytes (this is the role for setlocale)
  • you must tell the terminal what charset it should render (this is the role for chcp in Windows console)

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.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
0

I found out the cause, the wstring-to-string conversion is no problem, the problem was I used CA2T to convert the Chinese punctuation mark and it failed. So it showed "£¡" in the UI finally.

By means of mbstowcs, the counterpart of wcstombs, it would work.

enter image description here

Zhang
  • 3,030
  • 2
  • 14
  • 31