1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
| std::string UnicodeToANSI(const std::wstring &str, UINT iCodePage = CP_ACP) { std::string strRes; int iSize = ::WideCharToMultiByte(iCodePage, 0, str.c_str(), -1, NULL, 0, NULL, NULL);
if (iSize == 0) return strRes;
char *szBuf = new (std::nothrow) char[iSize]; if (!szBuf) return strRes; memset(szBuf, 0, iSize);
::WideCharToMultiByte(iCodePage, 0, str.c_str(), -1, szBuf, iSize, NULL, NULL);
strRes = szBuf; delete[] szBuf;
return strRes; }
std::wstring ANSIToUnicode(const std::string &str, UINT iCodePage = CP_ACP) { std::wstring strRes;
int iSize = ::MultiByteToWideChar(iCodePage, 0, str.c_str(), -1, NULL, 0);
if (iSize == 0) return strRes;
wchar_t *szBuf = new (std::nothrow) wchar_t[iSize]; if (!szBuf) return strRes; memset(szBuf, 0, iSize * sizeof(wchar_t));
::MultiByteToWideChar(iCodePage, 0, str.c_str(), -1, szBuf, iSize);
strRes = szBuf; delete[] szBuf;
return strRes; }
std::string UnicodeToUTF8(const std::wstring &str) { std::string strRes;
int iSize = ::WideCharToMultiByte(CP_UTF8, 0, str.c_str(), -1, NULL, 0, NULL, NULL);
if (iSize == 0) return strRes;
char *szBuf = new (std::nothrow) char[iSize]; if (!szBuf) return strRes; memset(szBuf, 0, iSize);
::WideCharToMultiByte(CP_UTF8, 0, str.c_str(), -1, szBuf, iSize, NULL, NULL);
strRes = szBuf; delete[] szBuf;
return strRes; }
std::string UnicodeToUTF8BOM(const std::wstring &str) { std::string strRes;
int iSize = ::WideCharToMultiByte(CP_UTF8, 0, str.c_str(), -1, NULL, 0, NULL, NULL);
if (iSize == 0) return strRes;
unsigned char *szBuf = new (std::nothrow) unsigned char[iSize + 3]; if (!szBuf) return strRes; memset(szBuf, 0, iSize + 3);
if (::WideCharToMultiByte(CP_UTF8, 0, str.c_str(), -1, (LPSTR)(szBuf + 3), iSize, NULL, NULL) > 0) { szBuf[0] = 0xEF; szBuf[1] = 0xBB; szBuf[2] = 0xBF; }
strRes = (char*)szBuf; delete[] szBuf;
return strRes; }
std::wstring UTF8ToUnicode(const std::string &str) { std::wstring strRes; int iSize = ::MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);
if (iSize == 0) return strRes;
wchar_t *szBuf = new (std::nothrow) wchar_t[iSize]; if (!szBuf) return strRes; memset(szBuf, 0, iSize * sizeof(wchar_t)); ::MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, szBuf, iSize);
strRes = szBuf; delete[] szBuf;
return strRes; }
std::string ANSIToUTF8(const std::string &str, UINT iCodePage = CP_ACP) { return UnicodeToUTF8(ANSIToUnicode(str, iCodePage)); }
std::string ANSIToUTF8BOM(const std::string &str, UINT iCodePage = CP_ACP) { return UnicodeToUTF8BOM(ANSIToUnicode(str, iCodePage)); }
std::string UTF8ToANSI(const std::string &str, UINT iCodePage = CP_ACP) { return UnicodeToANSI(UTF8ToUnicode(str), iCodePage); }
|