vc中字符编码转换
一直用php开发,在php开发用字符编码转换很简单只要用iconv函数就可以实现,最近的项目要把vc的数据提交给php处理,在返回给vc结果, 问题来了,vc项目的编码是GB2312,而PHP程序是utf-8编码,导致数据乱码。 看到乱码第一感觉就是编码问题了。 vc中有像iconv这样的函数吗?查msdn没有。恩,看来还是php好用啊。不亏是我喜欢的语言。 vc中怎么实现呢?要utf8->gb2312谷歌到的结果是,要先把utf8转换为unicode编码,在把unicode转换成GB2312。也就是utf8 ->unicode ->gb2312这个过程。 如果要从Gb2312->utf8呢,就类似上面那样了,也就是gb2312->unicode ->utf-8这个过程。至于为什么要先转换成unicode编码呢?我自己也还不清楚。可能和什么是 utf8,gb2312编码有关吧,这里有变文章介绍这些编码。http://www.linuxforum.net/books/UTF-8-Unicode.html 好了看看vc中是怎么实现utf8到unicode编码的。可以用这个函数MultiByteToWideChar() 从unicode到gb2312是用WideCharToMultiByte()这个函数。 我把它写成了一个iconv()函数参数和php的一样下面是代码。
转自:http://www.cnblogs.com/phpzxh/archive/2009/09/16/1568130.html
MultiByteToWideChar和WideCharToMultiByte用法详解
http://www.cppblog.com/sunraiing9/archive/2007/03/21/20281.html
----- 转自:http://bbs.csdn.net/topics/190037999
CString str =
"A string here"
;
LPWSTR
lpszW =
new
WCHAR
[255];
LPTSTR
lpStr = str.GetBuffer( str.GetLength() ); //CString字符串 赋值 指针字符串 要使用 GetBuffer 方法。
int
nLen = MultiByteToWideChar(CP_ACP, 0,lpStr, -1, NULL, NULL); //计算单字节字符串转换为宽字符串需要多少数组空间。
MultiByteToWideChar(CP_ACP, 0, lpStr, -1, lpszW, nLen);
AFunctionUsesWCHAR( lpszW );
delete
[] lpszW;