If you're not trying to write something portable, then you can use the following Windows specific code:
Code:
#include <windows.h>
#include <string>
#include <vector>
std::wstring str_to_wstr(const std::string &str, UINT cp = CP_ACP)
{
int len = MultiByteToWideChar(cp, 0, str.c_str(), str.length(), 0, 0);
if (!len)
return L"ErrorA2W";
std::vector<wchar_t> wbuff(len + 1);
// NOTE: this does not NULL terminate the string in wbuff, but this is ok
// since it was zero-initialized in the vector constructor
if (!MultiByteToWideChar(cp, 0, str.c_str(), str.length(), &wbuff[0], len))
return L"ErrorA2W";
return &wbuff[0];
}//str_to_wstr
std::string wstr_to_str(const std::wstring &wstr, UINT cp = CP_ACP)
{
int len = WideCharToMultiByte(cp, 0, wstr.c_str(), wstr.length(),
0, 0, 0, 0);
if (!len)
return "ErrorW2A";
std::vector<char> abuff(len + 1);
// NOTE: this does not NULL terminate the string in abuff, but this is ok
// since it was zero-initialized in the vector constructor
if (!WideCharToMultiByte(cp, 0, wstr.c_str(), wstr.length(),
&abuff[0], len, 0, 0))
{
return "ErrorW2A";
}//if
return &abuff[0];
}//wstr_to_str
Windows codepage 936 is for GBK, which is an extension of GB2312.
Code:
string gbk;
...
wstring utf16 = str_to_wstr(gbk, 936);
string utf8 = wstr_to_str(utf16, CP_UTF8);
gg