Windows平台下的编码转换类
ANSI和UNICODE和UTF8的互相转换

Charset.h

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#pragma once
#include <tchar.h>
#include <stringapiset.h>
#include <stdlib.h>
#include <string>
class Charset
{
public:
static std::wstring ANSIToUnicode(const char* ansi)
{
int textlen = MultiByteToWideChar(CP_ACP, 0, ansi, -1, NULL, 0);
wchar_t * pBuffer = new wchar_t[textlen + 1];
memset(pBuffer, 0, (textlen + 1) * sizeof(wchar_t));
MultiByteToWideChar(CP_ACP, 0, ansi, -1, pBuffer, textlen);
std::wstring unicode(pBuffer);
delete[] pBuffer;
return unicode;
}

static BOOL ANSIToUnicode(wchar_t* dst, const int size, const char* ansi)
{
BOOL bResult{ FALSE };
int textlen = 0;
textlen = MultiByteToWideChar(CP_ACP, 0, ansi, -1, NULL, 0);
if (size > textlen)
{
// memset(dst, 0, (textlen + 1) * sizeof(wchar_t));
if (0 != MultiByteToWideChar(CP_ACP, 0, ansi, -1, dst, textlen))
{
bResult = TRUE;
}
dst[textlen] = 0;
}
return bResult;
}

static std::string UnicodeToANSI(const wchar_t *unicode)
{
int textlen = WideCharToMultiByte(CP_ACP, 0, unicode, -1, NULL, 0, NULL, NULL);
char * pBuffer = new char[textlen + 1];
memset(pBuffer, 0, sizeof(char) * (textlen + 1));
WideCharToMultiByte(CP_ACP, 0, unicode, -1, pBuffer, textlen, NULL, NULL);
std::string ansi{ pBuffer };
delete[] pBuffer;
return ansi;
}

static BOOL UnicodeToANSI(char* dst, const int size, const wchar_t *unicode)
{
BOOL bResult{ FALSE };
int textlen = WideCharToMultiByte(CP_ACP, 0, unicode, -1, NULL, 0, NULL, NULL);
if (size > textlen)
{
//memset(dst, 0, sizeof(char) * (textlen + 1));
if (0 != WideCharToMultiByte(CP_ACP, 0, unicode, -1, dst, textlen, NULL, NULL))
{
bResult = TRUE;
}
dst[textlen] = 0;
}
return bResult;
}

static std::wstring UTF8ToUnicode(const char* utf8)
{
int textlen = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
wchar_t * pBuffer = new wchar_t[textlen + 1];
memset(pBuffer, 0, (textlen + 1) * sizeof(wchar_t));
MultiByteToWideChar(CP_UTF8, 0, utf8, -1, (LPWSTR)pBuffer, textlen);
std::wstring unicode{ pBuffer };
delete[]pBuffer;
return unicode;
}

static std::wstring UTF8ToUnicode(const wchar_t* utf8)
{
std::string u = UnicodeToANSI(utf8);
int textlen = MultiByteToWideChar(CP_UTF8, 0, u.c_str(), -1, NULL, 0);
wchar_t * pBuffer = new wchar_t[textlen + 1];
memset(pBuffer, 0, (textlen + 1) * sizeof(wchar_t));
MultiByteToWideChar(CP_UTF8, 0, u.c_str(), -1, (LPWSTR)pBuffer, textlen);
std::wstring unicode{ pBuffer };
delete[]pBuffer;
return unicode;
}

static BOOL UTF8ToUnicode(wchar_t*dst, const int size, const char* utf8)
{
BOOL bResult{ FALSE };
int textlen = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
if (size > textlen)
{
// memset(dst, 0, (textlen + 1) * sizeof(wchar_t));
if (0 != MultiByteToWideChar(CP_UTF8, 0, utf8, -1, (LPWSTR)dst, textlen))
{
bResult = TRUE;
}
dst[textlen] = 0;
}
return bResult;
}

static std::string UnicodeToUTF8(const wchar_t *unicode)
{
int textlen = WideCharToMultiByte(CP_UTF8, 0, unicode, -1, NULL, 0, NULL, NULL);
char * pBuffer = new char[textlen + 1];
memset(pBuffer, 0, sizeof(char) * (textlen + 1));
WideCharToMultiByte(CP_UTF8, 0, unicode, -1, pBuffer, textlen, NULL, NULL);
std::string utf8{ pBuffer };
delete[] pBuffer;
return utf8;
}

static BOOL UnicodeToUTF8(char* dst, const int size, const wchar_t *unicode)
{
BOOL bResult{ FALSE };
int textlen = WideCharToMultiByte(CP_UTF8, 0, unicode, -1, NULL, 0, NULL, NULL);
if (size > textlen)
{
// memset(dst, 0, sizeof(char) * (textlen + 1));
if (0 != WideCharToMultiByte(CP_UTF8, 0, unicode, -1, dst, textlen, NULL, NULL))
{
bResult = TRUE;
}
dst[textlen] = 0;
}
return bResult;
}
// char utf8 -> unicode -> ansi
static std::string UTF8ToANSI(const char* utf8)
{
std::wstring unicode = UTF8ToUnicode(utf8);
std::string ansi = UnicodeToANSI(unicode.c_str());
return ansi;
}

#ifdef _UNICODE
// wchar utf8 -> char utf8 -> unicode
static std::wstring UTF8ToLocal(const wchar_t* utf8)
{
std::wstring unicode = UTF8ToUnicode(utf8);
return unicode;
}
#else
// char utf8 -> unicode -> ansi
static std::string UTF8ToLocal(const char* utf8)
{
std::string ansi = UTF8ToANSI(utf8);
return ansi;
}
#endif
};