一样,使用的是VS FOR LINUX进行测试。html
converting to execution character set: Invalid or incomplete multibyte or wide characterlinux
若是编译时候遇到该错误,则能够加上-finput-charset
-fexecute-charset
g++编译选项解决。由于linux下gcc但愿源文件是UTF-8格式,因此都改为UTF-8就行了。同时,也能够vs下装个forceUTF8插件。ide
搜了下,网上有说使用wprintf的,好比:函数
wchar_t c= L'中国';
wprintf(L"%c",c);测试
wprintf(L"%ls\n", L"中华人民共和国");编码
测试的时候发现wprintf没有打印任何东西。spa
有说使用locale loc("chs");的:.net
locale loc("chs");插件
wcout.imbue( loc );debug
也有说setlocale( LC_CTYPE,"chs");的
使用chs编码,运行时就报异常了,难道都没测过么???
最后仍是本身逐个调试解决了。以下:
setlocale(LC_ALL, "zh_CN.UTF-8"); wchar_t zh_cn = L'国'; wchar_t zh_cns[] = L"中国"; wchar_t another_w[sizeof(zh_cns)/sizeof(wchar_t)] = {0}; wcscpy(another_w, zh_cns); printf("%ls。。。....%ls。。。%lc\n", zh_cns, another_w,zh_cn);
加上-finput-charset
-fexecute-charset
g++编译选项或者在VS中把文件设置为UTF-8带签名格式便可。
输出:
中国。。。....中国。。。国
最后,宽字符的操做函数和char不一样,经常使用的能够参考http://www.cnblogs.com/lidabo/p/6912788.html。
上述设置后,环境中就是指定的字符集了,可是vs监视窗口仍然会是显示16进制,以下:
经查帖子https://blog.csdn.net/lainegates/article/details/72236321,将C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Packages\Debugger\Visualizers\stl.natvis的
将文件583-586行改成以下:
<DisplayString Condition="_Mypair._Myval2._Myres < _Mypair._Myval2._BUF_SIZE">{_Mypair._Myval2._Bx._Buf,s8}</DisplayString> <DisplayString Condition="_Mypair._Myval2._Myres >= _Mypair._Myval2._BUF_SIZE">{_Mypair._Myval2._Bx._Ptr,s8}</DisplayString> <StringView Condition="_Mypair._Myval2._Myres < _Mypair._Myval2._BUF_SIZE">_Mypair._Myval2._Bx._Buf,s8</StringView> <StringView Condition="_Mypair._Myval2._Myres >= _Mypair._Myval2._BUF_SIZE">_Mypair._Myval2._Bx._Ptr,s8</StringView>
以后,vs2015便可在debug时正常显示utf-8字符。TODO
还有个帖子说,“只须要将要显示的字符串拉到Watch中,并在变量后面添加,s8便可显示”,经测,这么作是不行的,监视窗口会报错,以下:
参考:
https://blog.csdn.net/wangsen_sc/article/details/6915995
https://blog.csdn.net/weiwangchao_/article/details/43453053
https://blog.csdn.net/angelxf/article/details/7803495
http://www.cplusplus.com/reference/cstdio/printf/(关键时候看官方手册)