字符串分割函数
vector<std::string> Split(const std::string& src, const std::string& sep)
{
std::vector<std::string> strVec;
std::string::size_type linepos = 0;
std::string::size_type endpos = 0;
while (endpos != std::string::npos)
{
endpos = src.find_first_of(sep, linepos);
strVec.push_back(src.substr(linepos, endpos - linepos));
linepos = endpos + 1;
}
return strVec;
}
Utf8ToGbk
std::string Utf8ToGbk(const char* utf8)
{
if (!utf8)
return "";
wchar_t* widechar = NULL;
char* multibyte = NULL;
int length = 0;
length = ::MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
if (length <= 0)
return "";
widechar = new wchar_t[length];
::MultiByteToWideChar(CP_UTF8, 0, utf8, -1, widechar, length);
length = ::WideCharToMultiByte(CP_ACP, 0, widechar, -1, NULL, 0, NULL, NULL);
if (length <= 0)
{
delete[] widechar;
return "";
}
multibyte = new char[length];
::WideCharToMultiByte(CP_ACP, 0, widechar, -1, multibyte, length, NULL, NULL);
delete[] widechar;
std::string gbk(multibyte);
delete[] multibyte;
return gbk;
}
gbkToUtf8
std::string GbkToUtf8(const char* gbk) const
{
if (!gbk)
return "";
wchar_t* widechar = NULL;
char* multibyte = NULL;
int length = 0;
length = ::MultiByteToWideChar(CP_ACP, 0, gbk, -1, NULL, 0);
if (length <= 0)
return "";
widechar = new wchar_t[length];
::MultiByteToWideChar(CP_ACP, 0, gbk, -1, widechar, length);
length = ::WideCharToMultiByte(CP_UTF8, 0, widechar, -1, NULL, 0, NULL, NULL);
if (length <= 0)
{
delete[] widechar;
return "";
}
multibyte = new char[length];
::WideCharToMultiByte(CP_UTF8, 0, widechar, -1, multibyte, length, NULL, NULL);
delete[] widechar;
std::string utf8(multibyte);
delete[] multibyte;
return utf8;
}