您的位置:首页 > 其它

Win32字符串编码格式转化

2016-07-29 16:03 260 查看
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。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  编码