-
Notifications
You must be signed in to change notification settings - Fork 0
/
strCoding.cpp
238 lines (197 loc) · 4.41 KB
/
strCoding.cpp
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include "StdAfx.h"
#include "strCoding.h"
//这是个类strCoding (strCoding.cpp文件)
strCoding::strCoding(void)
{
}
strCoding::~strCoding(void)
{
}
void strCoding::Gb2312ToUnicode(WCHAR* pOut, char *gbBuffer)
{
::MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, gbBuffer, 2, pOut, 1);
return;
}
void strCoding::UTF_8ToUnicode(WCHAR* pOut, char *pText)
{
char* uchar = (char *)pOut;
uchar[1] = ((pText[0] & 0x0F) << 4) + ((pText[1] >> 2) & 0x0F);
uchar[0] = ((pText[1] & 0x03) << 6) + (pText[2] & 0x3F);
return;
}
void strCoding::UnicodeToUTF_8(char* pOut, WCHAR* pText)
{
// 注意 WCHAR高低字的顺序,低字节在前,高字节在后
char* pchar = (char *)pText;
pOut[0] = (0xE0 | ((pchar[1] & 0xF0) >> 4));
pOut[1] = (0x80 | ((pchar[1] & 0x0F) << 2)) + ((pchar[0] & 0xC0) >> 6);
pOut[2] = (0x80 | (pchar[0] & 0x3F));
return;
}
void strCoding::UnicodeToGB2312(char* pOut, WCHAR uData)
{
WideCharToMultiByte(CP_ACP, NULL, &uData, 1, pOut, sizeof(WCHAR), NULL, NULL);
return;
}
//做为解Url使用
char strCoding::CharToInt(char ch){
if (ch >= '0' && ch <= '9')return (char)(ch - '0');
if (ch >= 'a' && ch <= 'f')return (char)(ch - 'a' + 10);
if (ch >= 'A' && ch <= 'F')return (char)(ch - 'A' + 10);
return -1;
}
char strCoding::StrToBin(char *str){
char tempWord[2];
char chn;
tempWord[0] = CharToInt(str[0]); //make the B to 11 -- 00001011
tempWord[1] = CharToInt(str[1]); //make the 0 to 0 -- 00000000
chn = (tempWord[0] << 4) | tempWord[1]; //to change the BO to 10110000
return chn;
}
//UTF_8 转gb2312
void strCoding::UTF_8ToGB2312(string &pOut, char *pText, int pLen)
{
char buf[4];
char* rst = new char[pLen + (pLen >> 2) + 2];
memset(buf, 0, 4);
memset(rst, 0, pLen + (pLen >> 2) + 2);
int i = 0;
int j = 0;
while (i < pLen)
{
if (*(pText + i) >= 0)
{
rst[j++] = pText[i++];
}
else
{
WCHAR Wtemp;
UTF_8ToUnicode(&Wtemp, pText + i);
UnicodeToGB2312(buf, Wtemp);
unsigned short int tmp = 0;
tmp = rst[j] = buf[0];
tmp = rst[j + 1] = buf[1];
tmp = rst[j + 2] = buf[2];
//newBuf[j] = Ctemp[0];
//newBuf[j + 1] = Ctemp[1];
i += 3;
j += 2;
}
}
rst[j] = '\0';
pOut = rst;
delete[]rst;
}
//GB2312 转为 UTF-8
void strCoding::GB2312ToUTF_8(string& pOut, char *pText, int pLen)
{
char buf[4];
memset(buf, 0, 4);
pOut.clear();
int i = 0;
while (i < pLen)
{
//如果是英文直接复制就可以
if (pText[i] >= 0)
{
char asciistr[2] = { 0 };
asciistr[0] = (pText[i++]);
pOut.append(asciistr);
}
else
{
WCHAR pbuffer;
Gb2312ToUnicode(&pbuffer, pText + i);
UnicodeToUTF_8(buf, &pbuffer);
pOut.append(buf);
i += 2;
}
}
return;
}
//把str编码为网页中的 GB2312 url encode ,英文不变,汉字双字节 如%3D%AE%88
string strCoding::UrlGB2312(char * str)
{
string dd;
size_t len = strlen(str);
for (size_t i = 0; i<len; i++)
{
if (isalnum((BYTE)str[i]))
{
char tempbuff[2];
sprintf(tempbuff, "%c", str[i]);
dd.append(tempbuff);
}
else if (isspace((BYTE)str[i]))
{
dd.append("+");
}
else
{
char tempbuff[4];
sprintf(tempbuff, "%%%X%X", ((BYTE*)str)[i] >> 4, ((BYTE*)str)[i] % 16);
dd.append(tempbuff);
}
}
return dd;
}
//把str编码为网页中的 UTF-8 url encode ,英文不变,汉字三字节 如%3D%AE%88
string strCoding::UrlUTF8(char * str)
{
string tt;
string dd;
GB2312ToUTF_8(tt, str, (int)strlen(str));
size_t len = tt.length();
for (size_t i = 0; i<len; i++)
{
if (isalnum((BYTE)tt.at(i)))
{
char tempbuff[2] = { 0 };
sprintf(tempbuff, "%c", (BYTE)tt.at(i));
dd.append(tempbuff);
}
else if (isspace((BYTE)tt.at(i)))
{
dd.append("+");
}
else
{
char tempbuff[4];
sprintf(tempbuff, "%%%X%X", ((BYTE)tt.at(i)) >> 4, ((BYTE)tt.at(i)) % 16);
dd.append(tempbuff);
}
}
return dd;
}
//把url GB2312解码
string strCoding::UrlGB2312Decode(string str)
{
string output = "";
char tmp[2];
int i = 0, idx = 0, ndx, len = str.length();
while (i<len){
if (str[i] == '%'){
tmp[0] = str[i + 1];
tmp[1] = str[i + 2];
output += StrToBin(tmp);
i = i + 3;
}
else if (str[i] == '+'){
output += ' ';
i++;
}
else{
output += str[i];
i++;
}
}
return output;
}
//把url utf8解码
string strCoding::UrlUTF8Decode(string str)
{
string output = "";
string temp = UrlGB2312Decode(str);//
UTF_8ToGB2312(output, (char *)temp.data(), strlen(temp.data()));
return output;
}