1 #pragma once 2 3 #include <iostream> 4 #include <string> 5 #include <windows.h> 6 7 using namespace std; 8 9 class strCoding 10 { 11 public: 12 strCoding(void); 13 ~strCoding(void); 14 15 // utf_8 转 gb2312 16 void UTF_8ToGB2312(string &pOut, char *pText, int pLen); 17 // gb2312 转 utf_8 18 void GB2312ToUTF_8(string& pOut, char *pText, int pLen); 19 // url_gb2312 编码 20 string UrlGB2312(char *str); 21 // url_utf8 编码 22 string UrlUTF8(char *str); 23 // url_utf8 解码 24 string UrlUTF8Decode(string str); 25 // url_gb2312 解码 26 string UrlGB2312Decode(string str); 27 28 private: 29 void Gb2312ToUnicode(WCHAR *pOut, char *gbBuffer); 30 void UTF_8ToUnicode(WCHAR *pOut, char *pText); 31 void UnicodeToUTF_8(char *pOut, WCHAR *pText); 32 void UnicodeToGB2312(char *pOut, WCHAR uData); 33 char CharToInt(char ch); 34 char StrToBin(char *str); 35 }; 36 37 38 strCoding::strCoding(void) 39 { 40 } 41 42 strCoding::~strCoding(void) 43 { 44 } 45 46 void strCoding::Gb2312ToUnicode(WCHAR *pOut, char *gbBuffer) 47 { 48 ::MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, gbBuffer, 2, pOut, 1); 49 return; 50 } 51 52 void strCoding::UTF_8ToUnicode(WCHAR *pOut, char *pText) 53 { 54 char *uchar = (char *)pOut; 55 56 uchar[1] = ((pText[0] & 0x0F) << 4) + ((pText[1] >> 2) & 0x0F); 57 uchar[0] = ((pText[1] & 0x03) << 6) + (pText[2] & 0x3F); 58 59 return; 60 } 61 62 void strCoding::UnicodeToUTF_8(char *pOut, WCHAR *pText) 63 { 64 // 注意 WCHAR 高低字的顺序,低字节在前,高字节在后 65 char *pchar = (char *)pText; 66 67 pOut[0] = (0xE0 | ((pchar[1] & 0xF0) >> 4)); 68 pOut[1] = (0x80 | ((pchar[1] & 0x0F) << 2)) + ((pchar[0] & 0xC0) >> 6); 69 pOut[2] = (0x80 | (pchar[0] & 0x3F)); 70 71 return; 72 } 73 74 void strCoding::UnicodeToGB2312(char *pOut, WCHAR uData) 75 { 76 WideCharToMultiByte(CP_ACP, NULL, &uData, 1, pOut, sizeof(WCHAR), NULL, NULL); 77 return; 78 } 79 80 // 做为解Url使用 81 char strCoding::CharToInt(char ch) 82 { 83 if(ch >= ‘0‘ && ch <= ‘9‘)return (char)(ch - ‘0‘); 84 if(ch >= ‘a‘ && ch <= ‘f‘)return (char)(ch - ‘a‘ + 10); 85 if(ch >= ‘A‘ && ch <= ‘F‘)return (char)(ch - ‘A‘ + 10); 86 return -1; 87 } 88 89 char strCoding::StrToBin(char *str) 90 { 91 char tempWord[2]; 92 char chn; 93 94 tempWord[0] = CharToInt(str[0]); // make the B to 11 -- 00001011 95 tempWord[1] = CharToInt(str[1]); // make the 0 to 0 -- 00000000 96 chn = (tempWord[0] << 4) | tempWord[1]; // to change the BO to 10110000 97 98 return chn; 99 } 100 101 // UTF_8 转 gb2312 102 void strCoding::UTF_8ToGB2312(string &pOut, char *pText, int pLen) 103 { 104 char buf[4]; 105 char *rst = new char[pLen + (pLen >> 2) + 2]; 106 memset(buf, 0, 4); 107 memset(rst, 0, pLen + (pLen >> 2) + 2); 108 109 int i =0; 110 int j = 0; 111 112 while(i < pLen) 113 { 114 if(*(pText + i) >= 0) 115 { 116 rst[j++] = pText[i++]; 117 } 118 else 119 { 120 WCHAR Wtemp; 121 122 UTF_8ToUnicode(&Wtemp, pText + i); 123 UnicodeToGB2312(buf, Wtemp); 124 125 unsigned short int tmp = 0; 126 tmp = rst[j] = buf[0]; 127 tmp = rst[j + 1] = buf[1]; 128 tmp = rst[j + 2] = buf[2]; 129 130 //newBuf[j] = Ctemp[0]; 131 //newBuf[j + 1] = Ctemp[1]; 132 133 i += 3; 134 j += 2; 135 } 136 } 137 rst[j] = ‘\0‘; 138 pOut = rst; 139 delete []rst; 140 } 141 142 // GB2312 转 UTF-8 143 void strCoding::GB2312ToUTF_8(string& pOut, char *pText, int pLen) 144 { 145 char buf[4]; 146 memset(buf, 0, 4); 147 148 pOut.clear(); 149 150 int i = 0; 151 while(i < pLen) 152 { 153 // 如果是英文直接复制就可以 154 if( pText[i] >= 0) 155 { 156 char asciistr[2] = {0}; 157 asciistr[0] = (pText[i++]); 158 pOut.append(asciistr); 159 } 160 else 161 { 162 WCHAR pbuffer; 163 Gb2312ToUnicode(&pbuffer, pText + i); 164 UnicodeToUTF_8(buf,&pbuffer); 165 pOut.append(buf); 166 i += 2; 167 } 168 } 169 return; 170 } 171 172 // 把str编码为网页中的 GB2312 url encode ,英文不变,汉字双字节 如%3D%AE%88 173 string strCoding::UrlGB2312(char *str) 174 { 175 string dd; 176 size_t len = strlen(str); 177 for (size_t i = 0; i < len; i++) 178 { 179 if(isalnum((BYTE)str[i])) 180 { 181 char tempbuff[2]; 182 sprintf(tempbuff, "%c", str[i]); 183 dd.append(tempbuff); 184 } 185 else if (isspace((BYTE)str[i])) 186 { 187 dd.append("+"); 188 } 189 else 190 { 191 char tempbuff[4]; 192 sprintf(tempbuff, "%%%X%X", ((BYTE*)str)[i] >>4, ((BYTE*)str)[i] % 16); 193 dd.append(tempbuff); 194 } 195 } 196 return dd; 197 } 198 199 // 把str编码为网页中的 UTF-8 url encode ,英文不变,汉字三字节 如%3D%AE%88 200 string strCoding::UrlUTF8(char *str) 201 { 202 string tt; 203 string dd; 204 GB2312ToUTF_8(tt, str, (int)strlen(str)); 205 206 size_t len=tt.length(); 207 for (size_t i = 0; i < len; i++) 208 { 209 if(isalnum((BYTE)tt.at(i))) 210 { 211 char tempbuff[2] = {0}; 212 sprintf(tempbuff, "%c", (BYTE)tt.at(i)); 213 dd.append(tempbuff); 214 } 215 else if (isspace((BYTE)tt.at(i))) 216 { 217 dd.append("+"); 218 } 219 else 220 { 221 char tempbuff[4]; 222 sprintf(tempbuff, "%%%X%X", ((BYTE)tt.at(i)) >> 4, ((BYTE)tt.at(i)) % 16); 223 dd.append(tempbuff); 224 } 225 } 226 return dd; 227 } 228 229 // 把url GB2312解码 230 string strCoding::UrlGB2312Decode(string str) 231 { 232 string output = ""; 233 char tmp[2]; 234 int i = 0, idx = 0, ndx, len = str.length(); 235 236 while(i<len) 237 { 238 if(str[i] == ‘%‘) 239 { 240 tmp[0] = str[i + 1]; 241 tmp[1] = str[i + 2]; 242 output += StrToBin(tmp); 243 i = i + 3; 244 } 245 else if(str[i] == ‘+‘) 246 { 247 output += ‘ ‘; 248 i++; 249 } 250 else 251 { 252 output += str[i]; 253 i++; 254 } 255 } 256 return output; 257 } 258 259 // 把url utf8解码 260 string strCoding::UrlUTF8Decode(string str) 261 { 262 string output = ""; 263 string temp = UrlGB2312Decode(str); 264 UTF_8ToGB2312(output, (char *)temp.data(), strlen(temp.data())); 265 return output; 266 }
原文:https://www.cnblogs.com/hoy0a1d/p/8998245.html