字符串UTF-8与ASCII相互转换
2005-12-10 04:33
369 查看
LPSTR Convert(LPCSTR str, int sourceCodepage, int targetCodepage)
{
int len=_tcslen(str);
int unicodeLen=MultiByteToWideChar(sourceCodepage,0,str,-1,NULL,0);
wchar_t* pUnicode;
pUnicode=new wchar_t[unicodeLen+1];
memset(pUnicode,0,(unicodeLen+1)*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+1];
memset(pTargetData,0,targetLen+1);
WideCharToMultiByte(targetCodepage,0,(LPWSTR)pUnicode,-1,(char *)pTargetData,targetLen,NULL,NULL);
LPSTR szResult=new TCHAR[targetLen+1];
wsprintf(szResult,"%s",pTargetData);
delete[] pUnicode;
delete[] pTargetData;
return szResult;
}
用法
编码
Convert(str,936,CP_UTF8);
解码
Convert(str,CP_UTF8,936);
{
int len=_tcslen(str);
int unicodeLen=MultiByteToWideChar(sourceCodepage,0,str,-1,NULL,0);
wchar_t* pUnicode;
pUnicode=new wchar_t[unicodeLen+1];
memset(pUnicode,0,(unicodeLen+1)*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+1];
memset(pTargetData,0,targetLen+1);
WideCharToMultiByte(targetCodepage,0,(LPWSTR)pUnicode,-1,(char *)pTargetData,targetLen,NULL,NULL);
LPSTR szResult=new TCHAR[targetLen+1];
wsprintf(szResult,"%s",pTargetData);
delete[] pUnicode;
delete[] pTargetData;
return szResult;
}
用法
编码
Convert(str,936,CP_UTF8);
解码
Convert(str,CP_UTF8,936);
相关文章推荐
- ASCII,UTF-8,Unicode字符串相互转换
- windows ASCII,UTF-8,Unicode字符串相互转换
- ASCII,UTF-8,Unicode字符串相互转换
- ASCII,UTF-8,Unicode字符串相互转换
- C++编程:ASCII,UTF-8,Unicode字符串相互转换
- utf,unicode,ascii字符串之间的相互转换
- ASCII,UTF-8,Unicode字符串相互转换(转)
- ASCII,UTF-8,Unicode字符串相互转换(转)
- 也说VC下 UNICODE和ASCII字符串相互转换
- Python的ASCII, GB2312, Unicode , UTF-8 相互转换
- Python的ASCII, GB2312, Unicode , UTF-8 相互转换
- 16进制串与ASCII字符串相互转换
- C# 八进制数值字符串转换成ASCII 字符串相互转换
- 字符串编码格式相互转换Unicode --UTF-8 -- GBK
- ANSI,UNICODE, UTF-8 字符串之间的相互转换
- Java初学习 - 字符串与ASCII相互转换
- 单片机与安卓(java)字符串与字节,ascii之间的相互转换
- Python的ASCII, GB2312, Unicode , UTF-8 相互转换
- Python的ASCII, UTF-8 相互转换
- Java中字符串与ASCII相互转换