最近老是涉及到编码与解码的问题,GB2312转UTF-8,又或者UTF-8转GB2312
无意中在CSDN闲逛发现了一个CString 转UTF-8的思路,现摘寻下来,免得到时又找不着了.
CString UTF8Convert(CString str, int sourceCodepage, int targetCodepage)
{
int len=str.GetLength();
int unicodeLen=MultiByteToWideChar(sourceCodepage,0,str,-1,NULL,0);
wchar_t * pUnicode;
//pUnicode=new wchar_t[unicodeLen+1]; wrong!! //Note The return value includes the NULL termination character.
pUnicode=new wchar_t[unicodeLen];
memset(pUnicode,0,(unicodeLen)*sizeof(wchar_t));
MultiByteToWideChar(sourceCodepage,0,str,-1,(LPWSTR)pUnicode,unicodeLen);
BYTE * pTargetData;
int targetLen=WideCharToMultiByte(targetCodepage,0,(LPWSTR)pUnicode,-1,(char *)pTargetData,0,NULL,NULL);
pTargetData=new BYTE[targetLen];
memset(pTargetData,0,targetLen);
WideCharToMultiByte(targetCodepage,0,(LPWSTR)pUnicode,-1,(char *)pTargetData,targetLen,NULL,NULL);
CString rt;
rt.Format("%s",pTargetData);
delete pUnicode;
delete pTargetData;
return rt;
}
用法:
编码
UTF8Convert(str,936,CP_UTF8);
解码
UTF8Convert(str,CP_UTF8,936);
936:简体中文的编码页(code page)。