CharEncoding.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. #include "stdafx.h"
  2. #include "CharEncoding.h"
  3. WCHAR* CharEncoding::ASCII2UNICODE(IN LPCCH lpASCIIStr)
  4. {
  5. if ( lpASCIIStr == NULL )
  6. return NULL;
  7. // 获取宽字符字节数;
  8. int cchWideChar = MultiByteToWideChar(CP_ACP, 0, lpASCIIStr, -1, NULL, 0);
  9. if ( cchWideChar == 0)
  10. return NULL;
  11. // 转换成宽字符串;
  12. WCHAR *pWideChar = new WCHAR[cchWideChar + 1];
  13. memset(pWideChar, 0 , sizeof(WCHAR)*(cchWideChar + 1));
  14. int nWriteNum = MultiByteToWideChar(CP_ACP, 0, lpASCIIStr, -1, pWideChar, cchWideChar );
  15. if ( nWriteNum != cchWideChar)
  16. {
  17. if (pWideChar)
  18. delete []pWideChar;
  19. return NULL;
  20. }
  21. return pWideChar;
  22. }
  23. BOOL CharEncoding::ASCII2UNICODE(IN LPCCH lpASCIIStr, OUT PWCH pUNICODEStr, IN CONST INT& nUNICODEStrLen)
  24. {
  25. if ( lpASCIIStr == NULL )
  26. return FALSE;
  27. // 获取宽字符字节数;
  28. int cchWideChar = MultiByteToWideChar(CP_ACP, 0, lpASCIIStr, -1, NULL, 0);
  29. if ( cchWideChar == 0 || cchWideChar >= nUNICODEStrLen)
  30. return FALSE;
  31. // 转换成宽字符串;
  32. memset(pUNICODEStr, 0 , sizeof(WCHAR)*nUNICODEStrLen);
  33. int nWriteNum = MultiByteToWideChar(CP_ACP, 0, lpASCIIStr, -1, pUNICODEStr, cchWideChar );
  34. if ( nWriteNum != cchWideChar)
  35. return FALSE;
  36. return TRUE;
  37. }
  38. BOOL CharEncoding::ASCII2UNICODE(IN LPCCH lpASCIIStr, OUT wstring &strResult)
  39. {
  40. if ( lpASCIIStr == NULL )
  41. return FALSE;
  42. // 获取宽字符字节数;
  43. int cchWideChar = MultiByteToWideChar(CP_ACP, 0, lpASCIIStr, -1, NULL, 0);
  44. if ( cchWideChar == 0 )
  45. return FALSE;
  46. // 转换成宽字符串;
  47. WCHAR *pResult = new WCHAR[cchWideChar];
  48. memset(pResult, 0 , sizeof(WCHAR)*cchWideChar);
  49. int nWriteNum = MultiByteToWideChar(CP_ACP, 0, lpASCIIStr, -1, pResult, cchWideChar );
  50. if ( nWriteNum != cchWideChar)
  51. return FALSE;
  52. strResult = pResult;
  53. if ( pResult )
  54. delete[] pResult;
  55. return TRUE;
  56. }
  57. CHAR* CharEncoding::UNICODE2ASCII(IN LPWCH lpUNICODEStr)
  58. {
  59. if ( lpUNICODEStr == NULL )
  60. return NULL;
  61. // 获取多字节字符字节数;
  62. int cbMultiByte = WideCharToMultiByte(CP_OEMCP, 0, lpUNICODEStr, -1, NULL, 0, NULL, NULL);
  63. if ( cbMultiByte == 0 )
  64. return NULL;
  65. // 转换成多字节字符;
  66. CHAR *pMultiByteStr = new CHAR[cbMultiByte+1];
  67. memset(pMultiByteStr, 0, cbMultiByte + 1);
  68. int nWriteNum = WideCharToMultiByte(CP_OEMCP, 0, lpUNICODEStr, -1, pMultiByteStr, cbMultiByte, NULL, NULL);
  69. if (nWriteNum != cbMultiByte)
  70. {
  71. if (pMultiByteStr)
  72. delete []pMultiByteStr;
  73. return NULL;
  74. }
  75. return pMultiByteStr;
  76. }
  77. BOOL CharEncoding::UNICODE2ASCII(IN LPWCH lpUNICODEStr, OUT LPCH pASCIIStr, IN CONST INT& nASCIIStrLen)
  78. {
  79. if ( lpUNICODEStr == NULL )
  80. return FALSE;
  81. // 获取多字节字符字节数;
  82. int cbMultiByte = WideCharToMultiByte(CP_OEMCP, 0, lpUNICODEStr, -1, NULL, 0, NULL, NULL);
  83. if ( cbMultiByte == 0 || cbMultiByte >= nASCIIStrLen )
  84. return FALSE;
  85. // 转换成多字节字符;
  86. memset((void*)pASCIIStr, 0, nASCIIStrLen);
  87. int nWriteNum = WideCharToMultiByte(CP_OEMCP, 0, lpUNICODEStr, -1, pASCIIStr, cbMultiByte, NULL, NULL);
  88. if (nWriteNum != cbMultiByte)
  89. {
  90. return FALSE;
  91. }
  92. return TRUE;
  93. }
  94. BOOL CharEncoding::UNICODE2ASCII(IN LPWCH lpUNICODEStr, OUT string &strResult)
  95. {
  96. if ( lpUNICODEStr == NULL )
  97. return FALSE;
  98. // 获取多字节字符字节数;
  99. int cbMultiByte = WideCharToMultiByte(CP_OEMCP, 0, lpUNICODEStr, -1, NULL, 0, NULL, NULL);
  100. if ( cbMultiByte == 0 )
  101. return FALSE;
  102. // 转换成多字节字符;
  103. CHAR* pResult = new CHAR[cbMultiByte];
  104. memset(pResult, 0, cbMultiByte);
  105. int nWriteNum = WideCharToMultiByte(CP_OEMCP, 0, lpUNICODEStr, -1, pResult, cbMultiByte, NULL, NULL);
  106. if (nWriteNum != cbMultiByte)
  107. return FALSE;
  108. strResult = pResult;
  109. if ( pResult )
  110. delete[] pResult;
  111. return TRUE;
  112. }
  113. CHAR* CharEncoding::UNICODE2UTF8(IN LPWCH lpUNICODEStr)
  114. {
  115. if ( lpUNICODEStr == NULL )
  116. return NULL;
  117. // 获取多字节字符字节数;
  118. int cbMultiByte = WideCharToMultiByte(CP_UTF8, 0, lpUNICODEStr, -1, NULL, 0, NULL, NULL);
  119. if ( cbMultiByte == 0 )
  120. return NULL;
  121. // 转换成多字节字符;
  122. CHAR* pMultiByteStr = new CHAR[cbMultiByte+1];
  123. memset(pMultiByteStr, 0, cbMultiByte + 1);
  124. int nWriteNum = WideCharToMultiByte(CP_UTF8, 0, lpUNICODEStr, -1, pMultiByteStr, cbMultiByte, NULL, NULL);
  125. if (nWriteNum != cbMultiByte)
  126. {
  127. if (pMultiByteStr)
  128. delete []pMultiByteStr;
  129. return NULL;
  130. }
  131. return pMultiByteStr;
  132. }
  133. BOOL CharEncoding::UNICODE2UTF8(IN LPWCH lpUNICODEStr, OUT LPCH pUTF8Str, IN CONST INT& nUTF8StrLen)
  134. {
  135. if ( lpUNICODEStr == NULL )
  136. return FALSE;
  137. // 获取多字节字符字节数;
  138. int cbMultiByte = WideCharToMultiByte(CP_UTF8, 0, lpUNICODEStr, -1, NULL, 0, NULL, NULL);
  139. if ( cbMultiByte == 0 || cbMultiByte >= nUTF8StrLen )
  140. return FALSE;
  141. // 转换成多字节字符;
  142. memset(pUTF8Str, 0, nUTF8StrLen);
  143. int nWriteNum = WideCharToMultiByte(CP_UTF8, 0, lpUNICODEStr, -1, pUTF8Str, cbMultiByte, NULL, NULL);
  144. if (nWriteNum != cbMultiByte)
  145. {
  146. return FALSE;
  147. }
  148. return TRUE;
  149. }
  150. BOOL CharEncoding::UNICODE2UTF8(IN LPWCH lpUNICODEStr, OUT string &strResult)
  151. {
  152. if ( lpUNICODEStr == NULL )
  153. return FALSE;
  154. // 获取多字节字符字节数;
  155. int cbMultiByte = WideCharToMultiByte(CP_UTF8, 0, lpUNICODEStr, -1, NULL, 0, NULL, NULL);
  156. if ( cbMultiByte == 0 )
  157. return FALSE;
  158. // 转换成多字节字符;
  159. CHAR *pResult = new CHAR[cbMultiByte];
  160. memset(pResult, 0, cbMultiByte);
  161. int nWriteNum = WideCharToMultiByte(CP_UTF8, 0, lpUNICODEStr, -1, pResult, cbMultiByte, NULL, NULL);
  162. strResult = pResult;
  163. if ( pResult )
  164. delete[] pResult;
  165. if (nWriteNum != cbMultiByte)
  166. return FALSE;
  167. return TRUE;
  168. }
  169. CHAR* CharEncoding::ASCII2UTF8(IN LPCCH lpASCIIStr)
  170. {
  171. // 将ASCII字符串转成UNICODE字符串;
  172. WCHAR* pWideChar = ASCII2UNICODE(lpASCIIStr);
  173. if ( pWideChar == NULL )
  174. return NULL;
  175. // 再将UICODE转成UTF8;
  176. CHAR* pUTF8 = UNICODE2UTF8(pWideChar);
  177. if ( pWideChar )
  178. delete []pWideChar;
  179. return pUTF8;
  180. }
  181. BOOL CharEncoding::ASCII2UTF8(IN LPCCH lpASCIIStr, OUT LPCH pUTF8Str, IN CONST INT& nUTF8StrLen)
  182. {
  183. // 将ASCII字符串转成UNICODE字符串;
  184. WCHAR* pWideChar = ASCII2UNICODE(lpASCIIStr);
  185. if ( pWideChar == NULL )
  186. return FALSE;
  187. // 再将UICODE转成UTF8;
  188. BOOL bResult = UNICODE2UTF8(pWideChar, pUTF8Str, nUTF8StrLen);
  189. if ( pWideChar )
  190. delete []pWideChar;
  191. return bResult;
  192. }
  193. BOOL CharEncoding::ASCII2UTF8(IN LPCCH lpASCIIStr, OUT string &strResult)
  194. {
  195. // 将ASCII字符串转成UNICODE字符串;
  196. WCHAR* pWideChar = ASCII2UNICODE(lpASCIIStr);
  197. if ( pWideChar == NULL )
  198. return FALSE;
  199. // 再将UICODE转成UTF8;
  200. BOOL bResult = UNICODE2UTF8(pWideChar, strResult);
  201. if ( pWideChar )
  202. delete []pWideChar;
  203. return bResult;
  204. }
  205. WCHAR* CharEncoding::UTF82UNICODE(IN LPCCH lpUTF8)
  206. {
  207. if ( lpUTF8 == NULL )
  208. return NULL;
  209. // 获取unicode字符数;
  210. int cchWideChar = MultiByteToWideChar(CP_UTF8, 0, lpUTF8, -1, NULL, 0);
  211. if ( cchWideChar == 0)
  212. return NULL;
  213. // 转换成宽字符串;
  214. WCHAR *pWideChar = new WCHAR[cchWideChar + 1];
  215. memset(pWideChar, 0 , sizeof(WCHAR)*(cchWideChar + 1));
  216. int nWriteNum = MultiByteToWideChar(CP_UTF8, 0, lpUTF8, -1, pWideChar, cchWideChar );
  217. if ( nWriteNum != cchWideChar)
  218. {
  219. if (pWideChar)
  220. delete []pWideChar;
  221. return NULL;
  222. }
  223. return pWideChar;
  224. }
  225. BOOL CharEncoding::UTF82UNICODE(IN LPCCH lpUTF8, OUT PWCH pUNICODEStr, IN CONST INT& nUNICODEStrLen)
  226. {
  227. if ( lpUTF8 == NULL )
  228. return FALSE;
  229. // 获取宽字符字节数;
  230. int cchWideChar = MultiByteToWideChar(CP_UTF8, 0, lpUTF8, -1, NULL, 0);
  231. if ( cchWideChar == 0 || cchWideChar >= nUNICODEStrLen)
  232. return FALSE;
  233. // 转换成宽字符串;
  234. memset(pUNICODEStr, 0 , sizeof(WCHAR)*nUNICODEStrLen);
  235. int nWriteNum = MultiByteToWideChar(CP_UTF8, 0, lpUTF8, -1, pUNICODEStr, cchWideChar );
  236. if ( nWriteNum != cchWideChar)
  237. return FALSE;
  238. return TRUE;
  239. }
  240. BOOL CharEncoding::UTF82UNICODE(IN LPCCH lpUTF8, OUT wstring &strResult)
  241. {
  242. if ( lpUTF8 == NULL )
  243. return FALSE;
  244. // 获取宽字符字节数;
  245. int cchWideChar = MultiByteToWideChar(CP_UTF8, 0, lpUTF8, -1, NULL, 0);
  246. if ( cchWideChar == 0 )
  247. return FALSE;
  248. // 转换成宽字符串;
  249. WCHAR* pResult = new WCHAR[cchWideChar];
  250. memset(pResult, 0 , sizeof(WCHAR)*cchWideChar);
  251. int nWriteNum = MultiByteToWideChar(CP_UTF8, 0, lpUTF8, -1, pResult, cchWideChar );
  252. if ( nWriteNum != cchWideChar)
  253. return FALSE;
  254. strResult = pResult;
  255. if ( pResult )
  256. delete[] pResult;
  257. return TRUE;
  258. }
  259. CHAR* CharEncoding::UTF82ASCII(IN LPCCH lpUTF8)
  260. {
  261. // 将ASCII字符串转成UNICODE字符串;
  262. WCHAR* pWideChar = UTF82UNICODE(lpUTF8);
  263. if ( pWideChar == NULL )
  264. return NULL;
  265. // 再将UICODE转成UTF8;
  266. CHAR* pUTF8 = UNICODE2ASCII(pWideChar);
  267. if ( pWideChar )
  268. delete []pWideChar;
  269. return pUTF8;
  270. }
  271. BOOL CharEncoding::UTF82ASCII(IN LPCCH lpUTF8, OUT LPCH pASCIIStr, IN CONST INT& nASCIIStrLen)
  272. {
  273. // 将ASCII字符串转成UNICODE字符串;
  274. WCHAR* pWideChar = UTF82UNICODE(lpUTF8);
  275. if ( pWideChar == NULL )
  276. return FALSE;
  277. // 再将UICODE转成UTF8;
  278. BOOL bResult = UNICODE2ASCII(pWideChar, pASCIIStr, nASCIIStrLen);
  279. if ( pWideChar )
  280. delete []pWideChar;
  281. return bResult;
  282. }
  283. BOOL CharEncoding::UTF82ASCII(IN LPCCH lpUTF8, OUT string &strResult)
  284. {
  285. // 将ASCII字符串转成UNICODE字符串;
  286. WCHAR* pWideChar = UTF82UNICODE(lpUTF8);
  287. if ( pWideChar == NULL )
  288. return FALSE;
  289. // 再将UICODE转成UTF8;
  290. BOOL bResult = UNICODE2ASCII(pWideChar, strResult);
  291. if ( pWideChar )
  292. delete []pWideChar;
  293. return bResult;
  294. }
  295. //做为解Url使用
  296. char CharEncoding::CharToInt(char ch)
  297. {
  298. if (ch >= '0' && ch <= '9')return (char)(ch - '0');
  299. if (ch >= 'a' && ch <= 'f')return (char)(ch - 'a' + 10);
  300. if (ch >= 'A' && ch <= 'F')return (char)(ch - 'A' + 10);
  301. return -1;
  302. }
  303. char CharEncoding::StrToBin(IN char (&str)[2])
  304. {
  305. char tempWord[2];
  306. char chn;
  307. tempWord[0] = CharToInt(str[0]); //make the B to 11 -- 00001011
  308. tempWord[1] = CharToInt(str[1]); //make the 0 to 0 -- 00000000
  309. chn = (tempWord[0] << 4) | tempWord[1]; //to change the BO to 10110000
  310. return chn;
  311. }
  312. //GB2312 转为 UTF-8
  313. void CharEncoding::GB2312ToUTF_8(string& pOut, const char *pText, int pLen)
  314. {
  315. char buf[4];
  316. memset(buf, 0, 4);
  317. pOut.clear();
  318. int i = 0;
  319. while (i < pLen)
  320. {
  321. //如果是英文直接复制就可以;
  322. if (pText[i] >= 0)
  323. {
  324. char asciistr[2] = { 0 };
  325. asciistr[0] = (pText[i++]);
  326. pOut.append(asciistr);
  327. }
  328. else
  329. {
  330. WCHAR pbuffer[2] = {0};
  331. MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, pText + i, 2, pbuffer, 1);
  332. UNICODE2UTF8(pbuffer, buf, 4);
  333. pOut.append(buf);
  334. i += 2;
  335. }
  336. }
  337. return;
  338. }
  339. /************************************************************************/
  340. /* 函数:[7/26/2016 IT];
  341. /* 描述:将字符串编码成为GB2312格式的URL;;
  342. /* 参数:;
  343. /* [IN] :;
  344. /* [OUT] :;
  345. /* [IN/OUT] :;
  346. /* 返回:void;
  347. /* 注意:;
  348. /* 示例:;
  349. /*
  350. /* 修改:;
  351. /* 日期:;
  352. /* 内容:;
  353. /************************************************************************/
  354. string CharEncoding::EnCode_GB2312URL(IN const CHAR* pText)
  355. {
  356. string dd;
  357. size_t len = strlen(pText);
  358. for (size_t i = 0; i < len; i++)
  359. {
  360. if (isalnum((BYTE)pText[i]))
  361. {
  362. char tempbuff[2];
  363. sprintf_s(tempbuff, "%c", pText[i]);
  364. dd.append(tempbuff);
  365. }
  366. else if (isspace((BYTE)pText[i]))
  367. {
  368. dd.append("+");
  369. }
  370. else
  371. {
  372. char tempbuff[4];
  373. sprintf_s(tempbuff, "%%%X%X", ((BYTE*)pText)[i] >> 4, ((BYTE*)pText)[i] % 16);
  374. dd.append(tempbuff);
  375. }
  376. }
  377. return dd;
  378. }
  379. void CharEncoding::EnCode_GB2312URL(IN CHAR* pText, OUT string& strResult)
  380. {
  381. size_t len = strlen(pText);
  382. for (size_t i = 0; i < len; i++)
  383. {
  384. if (isalnum((BYTE)pText[i]))
  385. {
  386. char tempbuff[2];
  387. sprintf_s(tempbuff, "%c", pText[i]);
  388. strResult.append(tempbuff);
  389. }
  390. else if (isspace((BYTE)pText[i]))
  391. {
  392. strResult.append("+");
  393. }
  394. else
  395. {
  396. char tempbuff[4];
  397. sprintf_s(tempbuff, "%%%X%X", ((BYTE*)pText)[i] >> 4, ((BYTE*)pText)[i] % 16);
  398. strResult.append(tempbuff);
  399. }
  400. }
  401. }
  402. /************************************************************************/
  403. /* 函数:[7/26/2016 IT];
  404. /* 描述:;
  405. /* 参数:;
  406. /* [IN] :;
  407. /* [OUT] :;
  408. /* [IN/OUT] :;
  409. /* 返回:void;
  410. /* 注意:;
  411. /* 示例:;
  412. /*
  413. /* 修改:;
  414. /* 日期:;
  415. /* 内容:;
  416. /************************************************************************/
  417. string CharEncoding::EnCode_UTF8URL(IN const CHAR* pText)
  418. {
  419. string tt = "";
  420. string dd = "";
  421. ASCII2UTF8(pText,tt);
  422. size_t len = tt.length();
  423. for (size_t i = 0; i < len; i++)
  424. {
  425. if (isalnum((BYTE)tt.at(i)))
  426. {
  427. char tempbuff[2] = { 0 };
  428. sprintf_s(tempbuff, "%c", (BYTE)tt.at(i));
  429. dd.append(tempbuff);
  430. }
  431. else if (isspace((BYTE)tt.at(i)))
  432. {
  433. dd.append("+");
  434. }
  435. else
  436. {
  437. char tempbuff[4];
  438. sprintf_s(tempbuff, "%%%X%X", ((BYTE)tt.at(i)) >> 4, ((BYTE)tt.at(i)) % 16);
  439. dd.append(tempbuff);
  440. }
  441. }
  442. return dd;
  443. }
  444. void CharEncoding::EnCode_UTF8URL(IN const CHAR* pText, OUT string& strResult)
  445. {
  446. string tt = "";
  447. ASCII2UTF8(pText,tt);
  448. size_t len = tt.length();
  449. for (size_t i = 0; i < len; i++)
  450. {
  451. if (isalnum((BYTE)tt.at(i)))
  452. {
  453. char tempbuff[2] = { 0 };
  454. sprintf_s(tempbuff, "%c", (BYTE)tt.at(i));
  455. strResult.append(tempbuff);
  456. }
  457. else if (isspace((BYTE)tt.at(i)))
  458. {
  459. strResult.append("+");
  460. }
  461. else
  462. {
  463. char tempbuff[4];
  464. sprintf_s(tempbuff, "%%%X%X", ((BYTE)tt.at(i)) >> 4, ((BYTE)tt.at(i)) % 16);
  465. strResult.append(tempbuff);
  466. }
  467. }
  468. }
  469. string CharEncoding::EnCode_UNICODEURL(IN const CHAR* pText)
  470. {
  471. if ( pText == NULL )
  472. return "";
  473. std::string strReturn;
  474. int nLength = strlen(pText)*4+1;
  475. WCHAR *pwchBuf = new WCHAR[nLength];
  476. memset(pwchBuf, 0, sizeof(WCHAR) * nLength);
  477. MultiByteToWideChar(CP_ACP, 0, pText, -1, (LPWSTR)pwchBuf, nLength);
  478. unsigned char h, l;
  479. char tempbuff[8] = {0};
  480. for (size_t i = 0; i < wcslen(pwchBuf); i++)
  481. {
  482. // 取高低字节;
  483. h = *((unsigned char*)pwchBuf+i*2+1);
  484. l = *((unsigned char*)pwchBuf+i*2);
  485. sprintf_s(tempbuff, 8, "\\u%02x%02x", h, l);
  486. strReturn += tempbuff;
  487. }
  488. delete[]pwchBuf;
  489. pwchBuf = NULL;
  490. return strReturn;
  491. }
  492. /************************************************************************/
  493. /* 函数:[7/26/2016 IT];
  494. /* 描述:;
  495. /* 参数:;
  496. /* [IN] :;
  497. /* [OUT] :;
  498. /* [IN/OUT] :;
  499. /* 返回:void;
  500. /* 注意:;
  501. /* 示例:;
  502. /*
  503. /* 修改:;
  504. /* 日期:;
  505. /* 内容:;
  506. /************************************************************************/
  507. string CharEncoding::DeCode_URLGB2312(IN const CHAR* pURLText)
  508. {
  509. string output = "";
  510. char tmp[2];
  511. int i = 0, idx = 0, len = strlen(pURLText);
  512. while (i < len){
  513. if (pURLText[i] == '%')
  514. {
  515. tmp[0] = pURLText[i + 1];
  516. tmp[1] = pURLText[i + 2];
  517. output += StrToBin(tmp);
  518. i = i + 3;
  519. }
  520. else if (pURLText[i] == '+')
  521. {
  522. output += ' ';
  523. i++;
  524. }
  525. else{
  526. output += pURLText[i];
  527. i++;
  528. }
  529. }
  530. return output;
  531. }
  532. void CharEncoding::DeCode_URLGB2312(IN const CHAR* pURLText, OUT string& strResult)
  533. {
  534. char tmp[2];
  535. int i = 0, idx = 0, len = strlen(pURLText);
  536. while (i < len){
  537. if (pURLText[i] == '%')
  538. {
  539. tmp[0] = pURLText[i + 1];
  540. tmp[1] = pURLText[i + 2];
  541. strResult += StrToBin(tmp);
  542. i = i + 3;
  543. }
  544. else if (pURLText[i] == '+')
  545. {
  546. strResult += ' ';
  547. i++;
  548. }
  549. else{
  550. strResult += pURLText[i];
  551. i++;
  552. }
  553. }
  554. }
  555. /************************************************************************/
  556. /* 函数:[7/26/2016 IT];
  557. /* 描述:;
  558. /* 参数:;
  559. /* [IN] :;
  560. /* [OUT] :;
  561. /* [IN/OUT] :;
  562. /* 返回:void;
  563. /* 注意:;
  564. /* 示例:;
  565. /*
  566. /* 修改:;
  567. /* 日期:;
  568. /* 内容:;
  569. /************************************************************************/
  570. string CharEncoding::DeCode_URLUTF8(IN const CHAR* pURLText)
  571. {
  572. string output = "";
  573. string temp = DeCode_URLGB2312(pURLText);
  574. UTF82ASCII(temp.c_str(), output);
  575. return output;
  576. }
  577. void CharEncoding::DeCode_URLUTF8(IN const CHAR* pURLText, OUT string& strResult)
  578. {
  579. string temp = DeCode_URLGB2312(pURLText);
  580. UTF82ASCII(temp.c_str(), strResult);
  581. }
  582. /************************************************************************/
  583. /* 函数:[7/26/2016 IT];
  584. /* 描述:;
  585. /* 参数:;
  586. /* [IN] :;
  587. /* [OUT] :;
  588. /* [IN/OUT] :;
  589. /* 返回:void;
  590. /* 注意:;
  591. /* 示例:;
  592. /*
  593. /* 修改:;
  594. /* 日期:;
  595. /* 内容:;
  596. /************************************************************************/
  597. string CharEncoding::DeCode_URLUNICODE(IN const CHAR* pURLText)
  598. {
  599. string str = pURLText;
  600. string strResult = "";
  601. INT nIndex = 0;
  602. string strTemp = "";
  603. while ( str.find_first_of("\\u") != string::npos )
  604. {
  605. nIndex = str.find_first_of("\\u");
  606. strResult.append(str.substr(0, nIndex));
  607. strTemp = str.substr(nIndex + 2, 4);
  608. str = str.substr(nIndex + 2 +4);
  609. CHAR szReturn[10] = {0};
  610. union __UNION_VAR_INT{
  611. BYTE ch[2];
  612. int value;
  613. }unionVarInt;
  614. unionVarInt.ch[0] = (CharToInt(strTemp.at(2)) << 4) | (CharToInt(strTemp.at(3)) & 0x00FF);
  615. unionVarInt.ch[1] = (CharToInt(strTemp.at(0)) << 4) | (CharToInt(strTemp.at(1)) & 0x00FF);
  616. WCHAR szWide[2] = {0};
  617. szWide[0] = unionVarInt.value;
  618. UNICODE2ASCII(szWide,szReturn,10);
  619. strResult.append(szReturn);
  620. }
  621. strResult.append(str);
  622. return strResult;
  623. }
  624. void CharEncoding::DeCode_URLUNICODE(IN const CHAR* pURLText, OUT string& strResult)
  625. {
  626. string str = pURLText;
  627. INT nIndex = 0;
  628. string strTemp = "";
  629. while ( str.find_first_of("\\u") != string::npos )
  630. {
  631. nIndex = str.find_first_of("\\u");
  632. strResult.append(str.substr(0, nIndex));
  633. strTemp = str.substr(nIndex + 2, 4);
  634. str = str.substr(nIndex + 2 +4);
  635. CHAR szReturn[10] = {0};
  636. union __UNION_VAR_INT{
  637. BYTE ch[2];
  638. int value;
  639. }unionVarInt;
  640. unionVarInt.ch[0] = (CharToInt(strTemp.at(2)) << 4) | (CharToInt(strTemp.at(3)) & 0x00FF);
  641. unionVarInt.ch[1] = (CharToInt(strTemp.at(0)) << 4) | (CharToInt(strTemp.at(1)) & 0x00FF);
  642. WCHAR szWide[2] = {0};
  643. szWide[0] = unionVarInt.value;
  644. UNICODE2ASCII(szWide,szReturn,10);
  645. strResult.append(szReturn);
  646. }
  647. strResult.append(str);
  648. }