首页 > 代码库 > [C/C++]_[VS2010源码中使用UTF8中文字符串被转码为ANSI的问题]

[C/C++]_[VS2010源码中使用UTF8中文字符串被转码为ANSI的问题]


场景:

1.本以为vs设置了源文件的UTF8编码,代码中出现的中文字符串就一定是utf8编码了,可惜不是,如果源码中出现了中文字符串,会在内存中转码为ANSI编码。

Unicode(UTF8带签名) 代码页(65001),从菜单->文件->高级保存选项 设置.


例子:

char path[] = "resources\\中文\\";
for(int i = 0; i < strlen(path); ++i)
{
	printf("0x%x,",(unsigned char)path[i]);
}

查看UTF8文件十六进制
0x72,0x65,0x73,0x6F,0x75,0x72,0x63,0x65,0x73,0x5C,0x5C,0xE4,0xB8,0xAD,0xE6,0x96,0x87,0x5C,0x5C


打印出来的内存:
0x72,0x65,0x73,0x6f,0x75,0x72,0x63,0x65,0x73,0x5c,0xd6,0xd0,0xce,0xc4,0x5c

明显"中文”的文件编码是
0xE4,0xB8,0xAD,0xE6,0x96,0x87

而载入内存且变成了,这个编码是ANSI编码,也就是本地编码(GB2312).
0xd6,0xd0,0xce,0xc4

结论就是: vs源码上不要出现utf8的中文字符串,会被转码为ansi编码.