About encoded proplems between python and C++

72 Views Asked by At

I use python3.8 and C++, G++ for C++. If I write Chinese(in Unicode) string in C++ program, It will show me wrong string when I run the program,like this:

#include <iostream>
using namespace std;

int main()
{
    cout << "你好" << endl;
    return 0;
}

But, run it:

D:\Desktop>g++ test.cpp -o test

D:\Desktop>test.exe
浣犲ソ

Although maybe you don't understand Chinese, you can see that it's not string I want.

If I change the encoded to "gbk",this problem was solved,but another proplem come out when I run python:

print("你好")

result:

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

By the way,this is a part of my vimrc(Gvim's config) about encoded:

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

How can I find a encoded that C++ and Python string can both right? I use Gvim to edit my C++ and Python code in Windows, thank you for your help!

0

There are 0 best solutions below