晕!!!UTF8、ANSI不能互相转换吗
使用下面的函数,将字符串"卡(" 转成UTF-8,变成了"鍗?",然后再试着从UTF-8转ANSI就不行了变成了“?”,难道不能互转吗?
于是上网下了一堆的转换工具,发现都是这个问题,难道开发这些工具的兄弟都没注意到这个bug吗^_^
//使用:Convert(strA_in,strB_out,CP_UTF8,CP_ACP)//UTF8转换ANSI
// Convert(strA_out,strB_in,CP_ACP,CP_UTF8)//ANSI转换UTF8
void Convert(const char *strIn, char *strOut, int sourceCodepage, int targetCodepage)
{
int len=lstrlen(strIn);
int unicodeLen=MultiByteToWideChar(sourceCodepage,0,strIn,-1,NULL,0);
wchar_t* pUnicode;
pUnicode=new wchar_t[unicodeLen+1];
memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));
MultiByteToWideChar(sourceCodepage,0,strIn,-1,(LPWSTR)pUnicode,unicodeLen);
BYTE * pTargetData = NULL;
int targetLen=WideCharToMultiByte(targetCodepage,0,(LPWSTR)pUnicode,-1,/*(char *)pTargetData*/NULL,0,NULL,NULL);
pTargetData=new BYTE[targetLen+1];
memset(pTargetData,0,sizeof( char )*(targetLen+1));
WideCharToMultiByte(targetCodepage,0,(LPWSTR)pUnicode,-1,(char *)pTargetData,targetLen,NULL,NULL);
lstrcpy(strOut,(char*)pTargetData);
delete pUnicode;
delete pTargetData;
}
于是上网下了一堆的转换工具,发现都是这个问题,难道开发这些工具的兄弟都没注意到这个bug吗^_^
//使用:Convert(strA_in,strB_out,CP_UTF8,CP_ACP)//UTF8转换ANSI
// Convert(strA_out,strB_in,CP_ACP,CP_UTF8)//ANSI转换UTF8
void Convert(const char *strIn, char *strOut, int sourceCodepage, int targetCodepage)
{
int len=lstrlen(strIn);
int unicodeLen=MultiByteToWideChar(sourceCodepage,0,strIn,-1,NULL,0);
wchar_t* pUnicode;
pUnicode=new wchar_t[unicodeLen+1];
memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));
MultiByteToWideChar(sourceCodepage,0,strIn,-1,(LPWSTR)pUnicode,unicodeLen);
BYTE * pTargetData = NULL;
int targetLen=WideCharToMultiByte(targetCodepage,0,(LPWSTR)pUnicode,-1,/*(char *)pTargetData*/NULL,0,NULL,NULL);
pTargetData=new BYTE[targetLen+1];
memset(pTargetData,0,sizeof( char )*(targetLen+1));
WideCharToMultiByte(targetCodepage,0,(LPWSTR)pUnicode,-1,(char *)pTargetData,targetLen,NULL,NULL);
lstrcpy(strOut,(char*)pTargetData);
delete pUnicode;
delete pTargetData;
}
...全文
相关推荐
C语言UTF8到ANSI和Unicode转换代码
使用C语言,实现UTF8、Unicode、ANSI字符集的互转,用C语言实现头文件和源文件,引入工程可以直接使用。如果在MFC项目中使用该代码,需要更改配置,压缩包中有详细说明。
lua utf8 unicode ansi 转换
require "lc" print(lc.help()); Simple Characters Transformation a2w(ansi to unicode) u2w(utf8 to unicode) w2a(unicode to ansi) w2u(unicode to utf8) u2a(utf8 to ansi) a2u(ansi to utf8) bstr(bytes of str) help(show this) wunoman@qq.com 2012/03/06
UTF8与ansi string转换处理(DELPHI7开发)
delphi 开发的UTF8 ansistring转换动态库。 通过DELPHI与C++测试。 前面上传的资源未经过仔细验证,有问题,不好意思,重新修正上传
ansi转换为utf8的工具
本人机器上装的 ,亲测可用 , 批量转换txt编码,。
UTF8-UNICODE-ANSI编码互相转换小软件
UTF8-UNICODE-ANSI之间相互转换的小工具,很适用的。


