#include <string>
#include <codecvt>
Using namespace std;
// utf8 to unicode
Wstring utf8_to_wstring(const string &str)
{
Wstring_convert<codecvt_utf8<wchar_t>> utf8_cvt;
Return utf8_cvt.from_bytes(str);
}
// unicode to utf8
String wstring_to_utf8(const wstring &str)
{
Wstring_convert<codecvt_utf8<wchar_t>> utf8_cvt;
Return utf8_cvt.to_bytes(str);
}
// gbk to unicode
Wstring gbk_to_wstring(const string &str)
{
Wstring_convert<codecvt<wchar_t, char, mbstate_t>> gbk_cvt(new codecvt<wchar_t, char, mbstate_t>("chs"));
Return gbk_cvt.from_bytes(str);
}
// unicode to gbk
String wstring_to_gbk(const wstring &str)
{
Wstring_convert<codecvt<wchar_t, char, mbstate_t>> gbk_cvt(new codecvt<wchar_t, char, mbstate_t>("chs"));
Return gbk_cvt.to_bytes(str);
}
// utf8 to gbk
String utf8_to_gbk(const string &str)
{
Wstring_convert<codecvt_utf8<wchar_t>> utf8_cvt; // utf8-"unicode converter
Wstring_convert<codecvt<wchar_t, char, mbstate_t>> gbk_cvt(new codecvt<wchar_t, char, mbstate_t>("chs")); // unicode-"gbk converter
Wstring t = utf8_cvt.from_bytes(str);
Return gbk_cvt.to_bytes(t);
}
// gbk to utf8
String gbk_to_utf8(const string &str)
{
Wstring_convert<codecvt_utf8<wchar_t>> utf8_cvt; // utf8-"unicode converter
Wstring_convert<codecvt<wchar_t, char, mbstate_t>> gbk_cvt(new codecvt<wchar_t, char, mbstate_t>("chs")); // unicode-"gbk converter
Wstring t = gbk_cvt.from_bytes(str);
Return utf8_cvt.to_bytes(t);
}
Int main()
{
Locale::global(locale(""));
String u8str = u8"I love China i love china";
String str = "I love China i love china";
Wstring w1 = gbk_to_wstring(str);
Str = wstring_to_gbk(w1);
Cout << str << endl;
Wcout << w1 << endl;
Return 0;
}
c++11 function transcoding