【问题标题】:Unicode chars in console through C++通过 C++ 在控制台中的 Unicode 字符
【发布时间】:2012-11-05 14:21:48
【问题描述】:

Windows 8 x64; 视觉工作室 2012;

我通过书本学习 C++。在这个论坛上,我发现 many themes 关于通过 C++ 读取\写入 Unicode 字符串。但是这个主题没有标记为已解决(???)。 C++ 中的问题真的很大吗?我尝试了不同的变体——它们对我不起作用:

#include<iostream>
#include<Windows.h>
#include <io.h>
#include <fcntl.h>
using namespace std;

int main() {
    // variant 1:
    wcout << L"Hello World!" << endl; // displayed
    wcout << L"Привет Мир!" << endl;// not displayed

    //**********************************************

    // variant 2:
    SetConsoleOutputCP(CP_UTF8);
    wchar_t s[] = L"Hello World (2)!";
    int bufferSize = WideCharToMultiByte(CP_UTF8, 0, 
        s, -1, NULL, 0, NULL, NULL);
    char* m = new char[bufferSize]; 
    WideCharToMultiByte(CP_UTF8, 0, s, -1, m, 
        bufferSize, NULL, NULL);

    wprintf(L"%S", m); // valid output
    wcout << endl;
    printf("%s", m); // valid output
    wcout << endl;

    wchar_t s2[] = L"Привет мир (2)!";
    int bufferSize2 = WideCharToMultiByte(CP_UTF8, 0, 
        s2, -1, NULL, 0, NULL, NULL);
    char* m2 = new char[bufferSize2]; 
    WideCharToMultiByte(CP_UTF8, 0, s2, -1, m2, 
        bufferSize2, NULL, NULL);

    wprintf(L"%S", m2); // invalid output
    wcout << endl;
    printf("%s", m2); // invalid output
    wcout << endl;
    //**********************************************

    // variant 3 (not working):
    _setmode(_fileno(stdout), _O_U16TEXT);
    wcout << L"Testing unicode -- English -- Ελληνικά"
        << "-- Español." << endl;

    return 0;
}

但它仅适用于英文字符... 屏幕:

如何通过 C++ 解决这个问题?

【问题讨论】:

  • 题外话,但是你怎么能把windows 8的窗口角曲线弄成那样

标签: c++ unicode console


【解决方案1】:

解决方法是执行

chcp 65001

在执行您的程序之前在cmd.exe 中(我不知道如何以编程方式执行此操作)。 65001 是 UTF8 编码的神奇值。可用于chcp 的其他代码页列表在这里:http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/chcp.mspx?mfr=true。 Cyrillic CP1251 的其他有趣值是 855。

并且不要忘记将控制台字体切换为 Lucida(默认字体不适用于 UTf-8)。

【讨论】:

  • 谢谢。我试过了,在运行我的 EXE 文件之前,但结果是一样的。 :(((
  • @Bush 我忘了说您需要使用cmd.exe 属性将控制台字体切换为Lucida。我很高兴听到这是否有效。
  • 别忘了将您的控制台字体切换为 Lucide 是的,现在可以使用了!谢谢!
  • 我相信SetConsoleOutputCP(CP_UTF8); 的作用与chcp 65001 部分相同。
  • 如果您查找 Boost.Nowide,您可以使用它来获得很好的修复。
【解决方案2】:

我发现了更简单的变体(无需更改代码页和字体):

#include<iostream>
#include<windows.h>

using namespace std;
int main()
{
    cout<<"Привет мир (1)!" << endl; // invalid output

    SetConsoleCP(GetACP());
    SetConsoleOutputCP(GetACP());

    cout<<"Привет мир (2)!" << endl; // valid output!

    return 0;
}

也许它不仅对我感兴趣。

附:但是...它在 CMD.EXE 中有效,但在 POWERSHELL.EXE 中无效。

【讨论】:

    【解决方案3】:

    我找到了更清晰的决定然后chcp 命令使用:

    // Getting the readable Cyrillic chars in the console window...
    setlocale(LC_ALL, "Russian");
    wcout << endl << L"Добро "; // UNICODE
    cout << "пожаловать!" << endl; // ANSI
    

    对于这两种情况,我都会得到可读的输出。

    【讨论】:

      【解决方案4】:

      哦~~对不起,下面是C语言。

      在 C++ 中你可以这样做:

      #include <iostream>
      #include <locale>
      using namespace std;
      int main(int argc, char *argv[])
      {
          locale::global(std::locale(""));
          wcout << L"Привет Мир!" << endl;
          return 0;
      }
      

      #include <locale.h>
      
      setlocale(LC_ALL,NULL);
      

      默认设置为 (LC_ALL,"C")。所以不能显示ASCII码以外的字符

      【讨论】:

      • 谢谢。我将它添加到我的 CPP 文件中,重新构建它,但结果是一样的。 :((( 不会说英语的人是如何解决这个问题的?
      • 可能在 cmd.exe 中显示 unicode 字符有问题,因为 cmd 不完全支持 unicode
      猜你喜欢
      • 2017-11-16
      • 1970-01-01
      • 1970-01-01
      • 2019-03-11
      • 2013-11-20
      • 2018-10-31
      • 2011-08-10
      相关资源
      最近更新 更多