我使用 python3.8 和 C++,G++ 用于 C++。如果我在 C++ 程序中编写中文(Unicode)字符串,当我运行程序时它会显示错误的字符串,如下所示:
#include <iostream>
using namespace std;
int main()
{
cout << "你好" << endl;
return 0;
}
但是,运行它:
D:\Desktop>g++ test.cpp -o test
D:\Desktop>test.exe
浣犲ソ
虽然可能你不懂中文,但你可以看出它不是我想要的字符串。
如果我将编码更改为“gbk”,这个问题就解决了,但是当我运行python时又出现了另一个问题:
print("你好")
结果:
File "test.py", line 1
SyntaxError: Non-UTF-8 code starting with '\xc4' in file test.py on line 1, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
顺便说一句,这是我关于编码的 vimrc(Gvim 的配置)的一部分:
set fileencodings=utf-8,gbk,ucs-bom,utf-16,utf-32,gbk,big5,gb18030,latin1,gb2312
set encoding=gbk
set termencoding=gb2312
set fileencoding=gbk
" This will make Python be errored
set fileencodings=utf-8,gbk,ucs-bom,utf-16,utf-32,gbk,big5,gb18030,latin1,gb2312
set encoding=utf-8
set termencoding=utf-8
set fileencoding=utf-8
" This will make C++ string wrong
如何找到 C++ 和 Python 字符串都可以正确编码的编码?我使用 Gvim 在 Windows 中编辑我的 C++ 和 Python 代码,感谢您的帮助!