16,471
社区成员
发帖
与我相关
我的任务
分享
int StringToUtf8(LPCTSTR strIn, LPSTR pstrUtf8, int iUTF8BuffLen, UINT cpCode=CP_THREAD_ACP)
{
int iConvRet = 0;
//strIn先转换到UNICODE
WCHAR *pWString = NULL;
#if (defined UNICODE || defined _UNICODE) //UNICODE
pWString = strIn;
#else //!UNICODE
int iSrcLen = MultiByteToWideChar(cpCode, 0, strIn, -1, NULL, 0);
pWString = new WCHAR[iSrcLen + 1];
MultiByteToWideChar(cpCode, 0, strIn, -1, pWString, iSrcLen);
pWString[iSrcLen] = 0;
#endif //
//UNICODE转换到UTF8
iConvRet = WideCharToMultiByte(CP_UTF8, 0, pWString, -1, pstrUtf8, iUTF8BuffLen, NULL, NULL);
//结束清理
#if !(defined UNICODE || defined _UNICODE)
delete [] pWString; pWString = NULL;
#endif //UNICODE
return iConvRet;
}
int Utf8ToString(LPCSTR strUtf8, LPTSTR pStrBuffer, int iStrBuffLen, UINT cpCode=CP_THREAD_ACP)
{
int iConvRet = 0;
#if (defined UNICODE || defined _UNICODE) //UNICODE
//UTF8 转换到 Unicode
iConvRet = MultiByteToWideChar(CP_UTF8, 0, strUtf8, -1, pStrBuffer, iStrBuffLen);
#else //!UNICODE
//UTF8 转换到 Unicode
int iSrcLen = MultiByteToWideChar(CP_UTF8, 0, strUtf8, -1, NULL, 0);
WCHAR *pWString = new WCHAR[iSrcLen + 1];
MultiByteToWideChar(CP_UTF8, 0, strUtf8, -1, pWString, iSrcLen);
pWString[iSrcLen] = 0;
//Unicode 转到多字节
iConvRet = WideCharToMultiByte(cpCode, 0, pWString, -1, pStrBuffer, iStrBuffLen, NULL, NULL);
//结束清理
delete [] pWString;
#endif //UNICODE
return iConvRet;
}
{
LPCTSTR szSrc = _T("防踩踏#19#res\\Media\\图片\\JPG\\防踩踏.jpg#JPG#5#1#1#0");
char chUtf8[128] = {0};
TCHAR chString[128]={0};
StringToUtf8(szSrc, chUtf8, 128);
Utf8ToString(chUtf8, chString, 128);
TRACE(_T("%s\n"), chString);
}