C++字符串编码必须处理字符串流,并处理相应的编码。否则容易产生乱码。字符串编码有几种办法, 一种是开源的libconv库(C语言库),一种是boost库(C++语言库),最后一种是windows平台库。 本文介绍最后一种,windows平台库的用法,拿来主义的读者可以直接拷贝,源代码如下:
#include
#include
#include
static std::wstring AsciiToUnicode(std::string asciiStr)
{
int widesize = MultiByteToWideChar(CP_ACP, 0, (char*)asciiStr.c_str(), -1, NULL, 0);
if (0 == widesize)
{
return std::wstring();
}
std::vector 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]);
}
static std::wstring Utf8ToUnicode(const std::string &s)
{
if (s.length() == 0) {
return std::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 std::wstring();
}
// convert
wchar_t* buf = new wchar_t[charcount];
MultiByteToWideChar(CP_UTF8, 0, s.c_str(), -1, buf, charcount);
std::wstring result(buf);
delete[] buf;
return result;
}
static std::string UnicodeToAscii(std::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 std::string();
}
// convert
char *buf = new char[charcount];
WideCharToMultiByte(CP_ACP, 0, unicodeStr.c_str(), -1, buf, charcount,
NULL, NULL);
std::string result(buf);
delete[] buf;
return result;
}
static std::string UnicodeToUtf8(std::wstring unicodeStr)
{
int utf8size = ::WideCharToMultiByte(CP_UTF8, 0, unicodeStr.c_str(), -1, NULL, 0, NULL, NULL);
if (0 == utf8size)
{
return std::string();
}
std::vector 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]);
}
static std::string AsciiToUtf8(std::string asic)
{
return UnicodeToUtf8(AsciiToUnicode(asic));
}
static std::string Utf8ToAscii(std::string utf8)
{
return UnicodeToAscii(Utf8ToUnicode(utf8));
}