c++ 无法输出 unicode 字符,即使我可以直接编写它们

c++ Unable to output unicode characters even though I can write them directly

本文关键字:我可以 输出 unicode 字符 c++      更新时间:2023-10-16

所以问题是我可以将粘贴的 unicode 字符(如棋子)直接复制到终端(我使用的是 debian jessie linux),但每当我编写 c++ 代码来做到这一点时,我都会得到这些这是我的代码

enter code here
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
using namespace std;
int main()
{
    setlocale(LC_ALL,"");
    wchar_t piece='♗';
    wcout<<piece;
}

我尝试使用字符的十六进制或十进制代码,但它不起作用我还使用 vim 进行编辑,它确实在我键入时显示字符。

没有

规范应该对wchar_t使用哪种编码。我需要使用 mbstowcs 函数来转换该字符。像这样,例如:

#include <iostream>
#include <clocale>
#include <cstdlib>
using namespace std;
int main(void) {
  setlocale(LC_ALL, "");
  wchar_t piece;
  mbstowcs(&piece, "♗", 1);
  wcout << piece << endl;
  return 0;
}

假设源文件编码与区域设置的编码匹配。

奇怪的是,

正常工作的是,将特殊字符放入字符串中,它非常简单,我什至没有想到使用它。

#include<iostream>
using namespace std;
int main()
{
    string piece="♗";
    cout<<piece;
}
相关文章: