晕!!!UTF8、ANSI不能互相转换吗

go16888888 go16888888 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
使用下面的函数,将字符串"卡(" 转成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;
}
...全文
评论 11
收藏 已收藏
分享
返回顶部
go16888888
C语言 70,039 成员 243,237 内容