//功能: 将Unicode字串转换为UTF8字串
//参数:
// strDownloadInfo: 要下载的app的信息(Unicode类型)
//返回值:
// 成功返回转换后的UTF8的string字串,失败则返回空字串
string UnicodeConversionUTF8(string strDownloadInfo)
{
string strResurt; // 返回的结构字符串
DWORD dwStrlen = 0; // 要获取的字节数的大小
char *strNew = NULL;
__try
{
if (0 == strDownloadInfo.size())
{
__leave;
}
if ((dwStrlen=::WideCharToMultiByte(CP_UTF8,0,CA2W(strDownloadInfo.data()),-1,NULL,0,NULL,NULL))==0)
{
__leave;
}
strNew = new char[dwStrlen+1];
if (NULL == strNew)
{
__leave;
}
if (0 == ::WideCharToMultiByte(CP_UTF8,0,CA2W(strDownloadInfo.data()),-1,strNew,dwStrlen,NULL,NULL))
{
__leave;
}
strResurt = strNew;
}
__finally
{
if (strNew != NULL)
{
delete[] strNew;
}
}
return strResurt;
}
// 功能:将UTF8字节转换为Unicode编码的字符串
// 参数:
// strUtf:要转换的UTF8的编码字符串
// 返回值:
// 成功后返回Unicode编码的字符串,失败则返回空字符串
string UTF8ConversionUnicode(string strUtf)
{
string strResurt; // 返回的结构字符串
DWORD dwStrlen = 0; // 要获取的字节数的大小
WCHAR *strNew = NULL;
__try
{
if (0 == strUtf.size())
{
__leave;
}
if (0 == (dwStrlen=::MultiByteToWideChar(CP_UTF8,0,strUtf.data(),-1,NULL,0)))
{
__leave;
}
strNew = new WCHAR[dwStrlen+1];
if (NULL == strNew)
{
__leave;
}
ZeroMemory(strNew,sizeof(TCHAR)*(dwStrlen+1));
if (0 == ::MultiByteToWideChar(CP_UTF8,0,strUtf.data(),-1,strNew,dwStrlen))
{
__leave;
}
strResurt = CW2A(strNew);
}
__finally
{
if (strNew != NULL)
{
delete[] strNew;
}
}
return strResurt;
}
字符串与UTF_8的相互转换
最新推荐文章于 2024-07-12 10:25:21 发布