std::wstring UTF8ToUnicode(const std::string& utf8string)
1 { 2 int widesize = ::MultiByteToWideChar(CP_UTF8, 0, utf8string.c_str(), -1, NULL, 0); 3 if (widesize == ERROR_NO_UNICODE_TRANSLATION) 4 { 5 throw std::exception("Invalid UTF-8 sequence."); 6 } 7 if (widesize == 0) 8 { 9 throw std::exception("Error in conversion."); 10 } 11 std::vector<wchar_t> resultstring(widesize); 12 int convresult = ::MultiByteToWideChar(CP_UTF8, 0, utf8string.c_str(), -1, &resultstring[0], widesize); 13 if (convresult != widesize) 14 { 15 throw std::exception("La falla!"); 16 } 17 return std::wstring(&resultstring[0]); 18 }
std::string WideByteToAcsi(std::wstring& wstrcode)
1 { 2 int asciisize = ::WideCharToMultiByte(CP_OEMCP, 0, wstrcode.c_str(), -1, NULL, 0, NULL, NULL); 3 if (asciisize == ERROR_NO_UNICODE_TRANSLATION) 4 { 5 throw std::exception("Invalid UTF-8 sequence."); 6 } 7 if (asciisize == 0) 8 { 9 throw std::exception("Error in conversion."); 10 } 11 std::vector<char> resultstring(asciisize); 12 int convresult = ::WideCharToMultiByte(CP_OEMCP, 0, wstrcode.c_str(), -1, &resultstring[0], asciisize, NULL, NULL); 13 if (convresult != asciisize) 14 { 15 throw std::exception("La falla!"); 16 } 17 return std::string(&resultstring[0]); 18 }
std::string UTF8ToASCII(std::string& strUtf8Code)
{ std::string strRet(""); //先把 utf8 转为 unicode std::wstring wstr = UTF8ToUnicode(strUtf8Code); //最后把 unicode 转为 ascii strRet = WideByteToAcsi(wstr); return strRet; }
std::string ASCIIToUTF8(std::string& strASCIICode)
1 { 2 std::string strOutUTF8 = ""; 3 WCHAR * str1; 4 int n = MultiByteToWideChar(CP_ACP, 0, strASCIICode.c_str(), -1, NULL, 0); 5 str1 = new WCHAR[n]; 6 MultiByteToWideChar(CP_ACP, 0, strASCIICode.c_str(), -1, str1, n); 7 n = WideCharToMultiByte(CP_UTF8, 0, str1, -1, NULL, 0, NULL, NULL); 8 char * str2 = new char[n]; 9 WideCharToMultiByte(CP_UTF8, 0, str1, -1, str2, n, NULL, NULL); 10 strOutUTF8 = str2; 11 delete[]str1; 12 str1 = NULL; 13 delete[]str2; 14 str2 = NULL; 15 return strOutUTF8; 16 }
//!!为了减少字符串的拷贝,改为使用shared_array,性能升级
#include <boost/shared_array.hpp>
#define CHAR_EMPTY_ARR_PTR boost::shared_array<char>(new char[1]{' '})
#define WCHAR_EMPTY_ARR_PTR boost::shared_array<wchar_t>(new wchar_t[1]{L' '})
boost::shared_array<char> UTF8ToASCII(std::string& strUtf8Code)
1 { 2 return std::move(UTF8ToASCII(strUtf8Code.c_str())); 3 }
boost::shared_array<char> UTF8ToASCII(const char* ch, int nLen /*= -1*/)
1 { 2 //先把 utf8 转为 unicode 3 auto wstr = UTF8ToUnicode(ch, nLen); 4 //最后把 unicode 转为 ascii 5 return std::move(WideByteToAcsi(wstr.get())); 6 }
boost::shared_array<char> ASCIIToUTF8(std::string& strASCIICode)
1 { 2 return std::move(ASCIIToUTF8(strASCIICode.c_str())); 3 }
boost::shared_array<char> ASCIIToUTF8(const char* ch, int nLen/* = -1*/)
1 { 2 int n = MultiByteToWideChar(CP_ACP, 0, ch, nLen, NULL, 0); 3 if (0 == n) 4 { 5 return CHAR_EMPTY_ARR_PTR; 6 } 7 boost::shared_array<wchar_t> ptrWtArr(new wchar_t[n]{ L'