wchar_t ws[] = L"我a1" 出现了 "converting to execution character set
从这个编译错误引发的一系列查找资料。
devc++ 的文本编辑器使用的编码不是utf8, 所以文本编辑器里的代码 【我】 使用的可能是gbk类的编码,
因此出现了 unicode中不可能出现的字节,从而报错。
总结一下:
C/C++中有这样两个编码概念:
source character set:源文件的编码
execution character set:执行环境的编码
source character set和你的文件中的character set不一致。编译时使用 finput-charset=GB2312 指定源文件的编码方案即可。
还有一种变态的避免 source charset 和 execution charset的办法: 转义字符。
【我】的unicode 值是 ced2, 因此 ws[] = L"\xce\xd2"; 绝对纯正!
字符编码涉及的范围:
1、 源文件中字符串的编码, 同样时字符串 【“我”】, 源文件以gbk保存或者以utf8 保存,在磁盘上的字节流肯定是不同的。
2、 【待验证】编译器的转换: 如果你的locale指定了unicode编码, 那么不管源文件中【我】是怎么存储的, 字符串在编译出来的东西里都是用unicode存储的。
3、 控制台的编码: 将字符串输出到控制台的时候,会根据系统设置的编码{ win中时不同的代码页, linux中则是设置系统的LC_*环境变量} 决定如何译码(根据指定编码方案规定的映射, 数字、字节流查出对应什么字符)因此如果 系统规定的编码 和 你输出的字符串编码格式不一致,就会出现乱码。
那么代码中应该如何处理字符串?
对于外部的输入 全部转为 unicode 存储, 输出的时候unicode要怎么转为系统的编码呢?
单纯的把字符串看作字节流,原样进来原样输出?
...
locale详解
http://blog.csdn.net/itblog/article/details/1513673
c++ unicode编程注意事项
http://www.cprogramming.com/tutorial/unicode.html
unicode 和 各类编码的关系
http://www.ruanyifeng.com/blog/2007/10/ascii_unicode_and_utf-8.html
http://www.searchtb.com/2012/04/chinese_encode.html
c locale 如何设置? 会影响那些函数的表现?
http://www.cplusplus.com/reference/clocale/
有哪些专门处理宽字符的库?
mfc qt都实现了自己的 Cstring QString 类,但是不容易自己抽出来用;
boost也有。
http://utfcpp.sourceforge.net/
http://puszcza.gnu.org.ua/software/microutf8/
c的话 有libiconv
但是 stackoverflow上面有回答说: utf8的目的就是尽量兼容以往的函数,因此可能并不需要一个新库?
UTF-8 is specially designed so that many byte-oriented string functions continue to work or only need minor modifications.
C's strstr
function, for instance, will work perfectly as long as both its inputs are valid, null-terminated UTF-8 strings.
strcpy
works fine as long as its input string starts at a character boundary (for instance the return value ofstrstr
).
So you may not even need a separate library!