string_converter.cpp
1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "stdafx.h"
#include "string_converter.h"
#include <locale>
#include <codecvt>
typedef std::wstring_convert<std::codecvt_utf8<wchar_t>> utf8_converter_t;
static utf8_converter_t utf8_converter;
typedef std::codecvt<wchar_t, char, std::mbstate_t> cvt; // Convert Unicode & GBK
static std::wstring_convert<cvt> gbk_converter(new cvt("CHS")); // Chinese (Simplified)_People's Republic of China.936
std::string wstr_2_str(const std::wstring& wstr)
{
return utf8_converter.to_bytes(wstr);
}
std::string wstr_2_str(const wchar_t* wstr)
{
return utf8_converter.to_bytes(wstr);
}
std::string wstr_2_str(const wchar_t* first, const wchar_t* last)
{
return utf8_converter.to_bytes(first, last);
}
std::wstring str_2_wstr(const std::string& str)
{
return utf8_converter.from_bytes(str);
}
std::wstring str_2_wstr(const char* str)
{
return utf8_converter.from_bytes(str);
}
std::wstring str_2_wstr(const char* first, const char* last)
{
return utf8_converter.from_bytes(first, last);
}
std::wstring gbk_2_wstr(const std::string& gbk_str)
{
return gbk_converter.from_bytes(gbk_str);
}
std::string wstr_2_gbk(const std::wstring& wstr)
{
return gbk_converter.to_bytes(wstr);
}