1、Unicode转UTF-8

 void CodeCovertTool::UNICODE_to_UTF8(const CString& unicodeString, std::string& str)
{//Unicode转UTF8
int stringLength = ::WideCharToMultiByte(CP_UTF8, NULL, unicodeString, wcslen(unicodeString), NULL, , NULL, NULL); char* buffer = new char[stringLength + ];
::WideCharToMultiByte(CP_UTF8, NULL, unicodeString, wcslen(unicodeString), buffer, stringLength, NULL, NULL);
buffer[stringLength] = '\0'; str = buffer; delete[] buffer;
}
 std::string CodeCovertTool::UnicodeToUtf8(const wchar_t* buf)
{//Unicode转UTF8
int len = ::WideCharToMultiByte(CP_UTF8, , buf, -, NULL, , NULL, NULL);
if (len == ) return ""; std::vector<char> utf8(len);
::WideCharToMultiByte(CP_UTF8, , buf, -, &utf8[], len, NULL, NULL); return &utf8[];
}

2、UTF-8转Unicode

 std::wstring CodeCovertTool::Utf8ToUnicode(const char* buf)
{//UTF8转Unicode
int len = ::MultiByteToWideChar(CP_UTF8, , buf, -, NULL, );
if (len == ) return _T(""); std::vector<wchar_t> unicode(len);
::MultiByteToWideChar(CP_UTF8, , buf, -, &unicode[], len); return &unicode[];
}
 CString CodeCovertTool::Utf8ToUnicode(const std::string &utf8_str)
{//UTF8转Unicode
int len;
len = MultiByteToWideChar(CP_UTF8, , (LPCSTR)utf8_str.c_str(), -, NULL,);
WCHAR * wszUnicode = new WCHAR[len+];
memset(wszUnicode, , len * + );
MultiByteToWideChar(CP_UTF8, , (LPCSTR)utf8_str.c_str(), -, wszUnicode, len);
CString ss=wszUnicode;
delete wszUnicode;
return ss;
}

3、Ansi转Unicode

 std::wstring CodeCovertTool::AnsiToUnicode(const char* buf)
{//Ansi转Unicode
int len = ::MultiByteToWideChar(CP_ACP, , buf, -, NULL, );
if (len == ) return L""; std::vector<wchar_t> unicode(len);
::MultiByteToWideChar(CP_ACP, , buf, -, &unicode[], len); return &unicode[];
}

4、Unicode转Ansi

 std::string CodeCovertTool::UnicodeToAnsi(const wchar_t* buf)
{//Unicode转Ansi
int len = ::WideCharToMultiByte(CP_ACP, , buf, -, NULL, , NULL, NULL);
if (len == ) return ""; std::vector<char> utf8(len);
::WideCharToMultiByte(CP_ACP, , buf, -, &utf8[], len, NULL, NULL); return &utf8[];
}
05-22 19:24