中文转UTF-8
// 转化函数
void MBSToUTF8(char * utf8, int size, const char* mbs)
{
if (!utf8 || !mbs)
return;
UINT nACP = GetACP();
int dwNum = MultiByteToWideChar(nACP, 0, mbs, -1, NULL, 0);
if (dwNum <= 0)
return;
wchar_t* pwText = NULL;
pwText = new wchar_t[dwNum + 1];
if (!pwText)
{
delete[] pwText;
return;
}
memset(pwText, 0, dwNum * 2 + 2);
if (MultiByteToWideChar(nACP, 0, mbs, -1, pwText, dwNum) == 0) // CP_ACP
{
delete[] pwText;
return;
}
int dwCount = WideCharToMultiByte(CP_UTF8, 0, pwText, -1, NULL, 0, NULL, NULL);
if (dwCount <= 0)
{
delete[] pwText;
return;
}
if (size < dwCount + 1)
{
delete[] pwText;
return;
}
memset(utf8, 0, dwCount + 1);
if (WideCharToMultiByte(CP_UTF8, 0, pwText, -1, utf8, dwCount, NULL, NULL) == 0)
{
delete[] pwText;
return;
}
delete[] pwText;
}
///
// 调用
{
CString sType = L"中文字符串";
char utf1[64] = { 0 }; // 根据中文字符串长度,调整utf1长度
MBSToUTF8(utf1, sizeof(utf1), CW2A(sType));
}
CString与UTF-8互相转化
std::string ConvertCStringToUTF8(CString strValue)
{
std::wstring wbuffer;
#ifdef _UNICODE
wbuffer.assign(strValue.GetString(), strValue.GetLength());
#else
/*
* 转换ANSI到UNICODE
* 获取转换后长度
*/
int length = ::MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, (LPCTSTR)strValue, -1, NULL, 0);
wbuffer.resize(length);
/* 转换 */
MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)strValue, -1, (LPWSTR)(wbuffer.data()), wbuffer.length());
#endif
/* 获取转换后长度 */
int length = WideCharToMultiByte(CP_UTF8, 0, wbuffer.data(), wbuffer.size(), NULL, 0, NULL, NULL);
/* 获取转换后内容 */
std::string buffer;
buffer.resize(length);
WideCharToMultiByte(CP_UTF8, 0, strValue, -1, (LPSTR)(buffer.data()), length, NULL, NULL);
return(buffer);
}
CString ConvertUTF8ToCString(std::string utf8str)
{
/* 预转换,得到所需空间的大小 */
int nLen = ::MultiByteToWideChar(CP_UTF8, NULL,
utf8str.data(), utf8str.size(), NULL, 0);
/* 转换为Unicode */
std::wstring wbuffer;
wbuffer.resize(nLen);
::MultiByteToWideChar(CP_UTF8, NULL, utf8str.data(), utf8str.size(),
(LPWSTR)(wbuffer.data()), wbuffer.length());
#ifdef UNICODE
return(CString(wbuffer.data(), wbuffer.length()));
#else
/*
* 转换为ANSI
* 得到转换后长度
*/
nLen = WideCharToMultiByte(CP_ACP, 0,
wbuffer.data(), wbuffer.length(), NULL, 0, NULL, NULL);
std::string ansistr;
ansistr.resize(nLen);
/* 把unicode转成ansi */
WideCharToMultiByte(CP_ACP, 0, (LPWSTR)(wbuffer.data()), wbuffer.length(),
(LPSTR)(ansistr.data()), ansistr.size(), NULL, NULL);
return(CString(ansistr.data(), ansistr.length()));
#endif
}
UTF-8转UTF-16:
wstring UTF8ToUTF16(const char* szIn)
{
wstring strResult;
if (szIn)
{
wchar_t* wszUTF16;
int len = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)szIn, -1, NULL, 0);
wszUTF16 = new wchar_t[len + 1];
memset(wszUTF16, 0, len * 2 + 2);
MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)szIn, -1, (LPWSTR)wszUTF16, len);
strResult = wstring(wszUTF16);
delete[]wszUTF16;
}
return strResult;
}