//多字节转Utf8
bool MBToUTF8(char* pu8, const char* pmb, int mLen)
{
// convert an MBCS string to widechar
int nLen = MultiByteToWideChar(CP_ACP, 0, pmb, mLen, NULL, 0);
WCHAR* lpszW = NULL;
try
{
lpszW = new WCHAR[nLen];
}
catch(bad_alloc )
{
return false;
}
int nRtn = MultiByteToWideChar(CP_ACP, 0, pmb, mLen, lpszW, nLen);
if(nRtn != nLen)
{
delete[] lpszW;
return false;
}
// convert an widechar string to utf8
int utf8Len = WideCharToMultiByte(CP_UTF8, 0, lpszW, nLen, NULL, 0, NULL, NULL);
if (utf8Len <= 0)
{
return false;
}
nRtn = WideCharToMultiByte(CP_UTF8, 0, lpszW, nLen, pu8, utf8Len, NULL, NULL);
delete[] lpszW;
*(pu8+utf8Len) = '\0';
if (nRtn != utf8Len)
{
return false;
}
return true;
}
多字节转UTF8
最新推荐文章于 2020-07-09 16:48:34 发布