Win32字符串编码格式转化
1.ascii转unicode
wstring asciiToUnicode(string asciiStr) { int widesize = MultiByteToWideChar(CP_ACP, 0, (char*)asciiStr.c_str(), -1, NULL, 0); if(0 == widesize) { return std::wstring(); } std::vector<wchar_t> resultstring(widesize); int count = MultiByteToWideChar(CP_ACP, 0, (char*)asciiStr.c_str(), -1, &resultstring[0], widesize); if(0 == count) { return std::wstring(); } return std::wstring(&resultstring[0]); }
2.utf8转unicode
std::wstring utf8ToUnicode(const std::string &s) { if (s.length() == 0) { return wstring(); } // compute the length of the buffer we"ll need int charcount = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), -1, NULL, 0); if (charcount == 0) { return wstring(); } // convert wchar_t* buf = new wchar_t[charcount]; MultiByteToWideChar(CP_UTF8, 0, s.c_str(), -1, buf, charcount); wstring result(buf); delete[] buf; return result; }
3.unicode 转ascii
string unicodeToAscii(wstring unicodeStr) { // compute the length of the buffer we"ll need int charcount = WideCharToMultiByte(CP_ACP, 0, unicodeStr.c_str(), -1, NULL, 0, NULL, NULL); if (charcount == 0) { return string(); } // convert char *buf = new char[charcount]; WideCharToMultiByte(CP_ACP, 0, unicodeStr.c_str(), -1, buf, charcount, NULL, NULL); string result(buf); delete[] buf; return result; }
4.unicode转utf8
string unicodeToUtf8(wstring unicodeStr) { int utf8size = ::WideCharToMultiByte(CP_UTF8, 0, unicodeStr.c_str(), -1, NULL, 0, NULL, NULL); if(0 == utf8size) { return std::string(); } std::vector<char> resultstr(utf8size); int count = ::WideCharToMultiByte(CP_UTF8, 0, unicodeStr.c_str(), -1, &resultstr[0], utf8size, NULL, NULL); if(0 == count) { return std::string(); } return std::string(&resultstr[0]); }
5.ascii转utf8
string PublicFunction::asicToUtf8(string asic) { return unicodeToUtf8(asciiToUnicode(asic)); }
6.utf8转ascii
string utf8ToAsic(string utf8) { return unicodeToAscii(utf8ToUnicode(utf8)); }
总结:转编码格式转化的时候,如果转化的双方不是unicode编码,那么一定要通过unicode做中转,也就是说一定要先转化成unicode,然后再转化成目标编码。
转化时怎样决定编码(使用CP_UTF8还是CP_ACP),只要是转unicode,那么如果是utf8转unicode(或者unicode转utf8),那么肯定用CP_UTF8;
如果是ascii转unicode(或者unicode转ascii),那么肯定用CP_ACP。
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。