下面在 windows visual studio 2019上验证,工程如下:
叫法说明:
加编译选项 /utf-8指的是
文件编码指的是cpp文件的编码格式
运行时编码
exe中字符串编码
字符 ‘我’在不同的编码中的对应数字
我, utf8, E6 88 91
我, ANSI, CE D2
我, utf16BE, 62 11
我, utf16LE, 11 62
结果如下列表
=========================================================================
加编译选项 /utf-8
文件编码u8bom 运行时编码 exe中字符串编码
std::string str1("/我std::string"); utf8 utf8
std::string str2(u8"u8/我std::string"); utf8 utf8
std::wstring str3(L"L/我std::string"); unicode unicode
加编译选项 /utf-8
文件编码u8 运行时编码 exe中字符串编码
std::string str1("/我std::string"); utf8 utf8
std::string str2(u8"u8/我std::string"); utf8 utf8
std::wstring str3(L"L/我std::string"); unicode unicode
加编译选项 /utf-8
文件编码ANSI 运行时编码 exe中字符串编码
std::string str1("/我std::string"); ANSI ANSI
std::string str2(u8"u8/我std::string"); ANSI ANSI
std::wstring str3(L"L/我std::string"); 乱码(/我std对应 2F 00 0E 00 73 00 74 00 64 00) 乱码(/我std对应 2F 00 0E 00 73 00 74 00 64 00)
=========================================================================
不加编译选项 /utf-8
文件编码u8bom 运行时编码 exe中字符串编码
std::string str1("/我std::string"); ANSI ANSI
std::string str2(u8"u8/我std::string"); utf8 utf8
std::wstring str3(L"L/我std::string"); unicode unicode
不加编译选项 /utf-8
文件编码u8 运行时编码 exe中字符串编码
std::string str1("/我std::string"); utf8 utf8
std::string str2(u8"u8/我std::string"); 乱码(/我std对应 2F E9 8E B4 E6 86 87 74 64) 乱码(/我std对应 2F E9 8E B4 E6 86 87 74 64)
std::wstring str3(L"L/我std::string"); 乱码(/我std对应 2F 00 B4 93 87 61 74 00 64 00) 乱码(/我std对应 2F 00 B4 93 87 61 74 00 64 00)
不加编译选项 /utf-8
文件编码ANSI 运行时编码 exe中字符串编码
std::string str1("/我std::string"); ANSI ANSI
std::string str2(u8"u8/我std::string"); utf8 utf8
std::wstring str3(L"L/我std::string"); unicode unicode
总结
考虑到跨平台编码 winsdows mac linux,似乎 文件编码保存成utf8 编译选项也指定成utf8,能比较的支持跨平台