CharEncoding.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  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. if (nWriteNum != cbMultiByte)
  163. return FALSE;
  164. strResult = pResult;
  165. if ( pResult )
  166. delete[] pResult;
  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 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. return "";
  472. }
  473. /************************************************************************/
  474. /* 函数:[7/26/2016 IT];
  475. /* 描述:;
  476. /* 参数:;
  477. /* [IN] :;
  478. /* [OUT] :;
  479. /* [IN/OUT] :;
  480. /* 返回:void;
  481. /* 注意:;
  482. /* 示例:;
  483. /*
  484. /* 修改:;
  485. /* 日期:;
  486. /* 内容:;
  487. /************************************************************************/
  488. string CharEncoding::DeCode_URLGB2312(IN const CHAR* pURLText)
  489. {
  490. string output = "";
  491. char tmp[2];
  492. int i = 0, idx = 0, len = strlen(pURLText);
  493. while (i < len){
  494. if (pURLText[i] == '%')
  495. {
  496. tmp[0] = pURLText[i + 1];
  497. tmp[1] = pURLText[i + 2];
  498. output += StrToBin(tmp);
  499. i = i + 3;
  500. }
  501. else if (pURLText[i] == '+')
  502. {
  503. output += ' ';
  504. i++;
  505. }
  506. else{
  507. output += pURLText[i];
  508. i++;
  509. }
  510. }
  511. return output;
  512. }
  513. void CharEncoding::DeCode_URLGB2312(IN const CHAR* pURLText, OUT string& strResult)
  514. {
  515. char tmp[2];
  516. int i = 0, idx = 0, len = strlen(pURLText);
  517. while (i < len){
  518. if (pURLText[i] == '%')
  519. {
  520. tmp[0] = pURLText[i + 1];
  521. tmp[1] = pURLText[i + 2];
  522. strResult += StrToBin(tmp);
  523. i = i + 3;
  524. }
  525. else if (pURLText[i] == '+')
  526. {
  527. strResult += ' ';
  528. i++;
  529. }
  530. else{
  531. strResult += pURLText[i];
  532. i++;
  533. }
  534. }
  535. }
  536. /************************************************************************/
  537. /* 函数:[7/26/2016 IT];
  538. /* 描述:;
  539. /* 参数:;
  540. /* [IN] :;
  541. /* [OUT] :;
  542. /* [IN/OUT] :;
  543. /* 返回:void;
  544. /* 注意:;
  545. /* 示例:;
  546. /*
  547. /* 修改:;
  548. /* 日期:;
  549. /* 内容:;
  550. /************************************************************************/
  551. string CharEncoding::DeCode_URLUTF8(IN const CHAR* pURLText)
  552. {
  553. string output = "";
  554. string temp = DeCode_URLGB2312(pURLText);
  555. UTF82ASCII(temp.c_str(), output);
  556. return output;
  557. }
  558. void CharEncoding::DeCode_URLUTF8(IN const CHAR* pURLText, OUT string& strResult)
  559. {
  560. string temp = DeCode_URLGB2312(pURLText);
  561. UTF82ASCII(temp.c_str(), strResult);
  562. }
  563. /************************************************************************/
  564. /* 函数:[7/26/2016 IT];
  565. /* 描述:;
  566. /* 参数:;
  567. /* [IN] :;
  568. /* [OUT] :;
  569. /* [IN/OUT] :;
  570. /* 返回:void;
  571. /* 注意:;
  572. /* 示例:;
  573. /*
  574. /* 修改:;
  575. /* 日期:;
  576. /* 内容:;
  577. /************************************************************************/
  578. string CharEncoding::DeCode_URLUNICODE(IN const CHAR* pURLText)
  579. {
  580. string str = pURLText;
  581. string strResult = "";
  582. INT nIndex = 0;
  583. string strTemp = "";
  584. while ( str.find_first_of("\\u") != string::npos )
  585. {
  586. nIndex = str.find_first_of("\\u");
  587. strResult.append(str.substr(0, nIndex));
  588. strTemp = str.substr(nIndex + 2, 4);
  589. str = str.substr(nIndex + 2 +4);
  590. CHAR szReturn[10] = {0};
  591. union __UNION_VAR_INT{
  592. BYTE ch[2];
  593. int value;
  594. }unionVarInt;
  595. unionVarInt.ch[0] = (CharToInt(strTemp.at(2)) << 4) | (CharToInt(strTemp.at(3)) & 0x00FF);
  596. unionVarInt.ch[1] = (CharToInt(strTemp.at(0)) << 4) | (CharToInt(strTemp.at(1)) & 0x00FF);
  597. WCHAR szWide[2] = {0};
  598. szWide[0] = unionVarInt.value;
  599. UNICODE2ASCII(szWide,szReturn,10);
  600. strResult.append(szReturn);
  601. }
  602. strResult.append(str);
  603. return strResult;
  604. }
  605. void CharEncoding::DeCode_URLUNICODE(IN const CHAR* pURLText, OUT string& strResult)
  606. {
  607. string str = pURLText;
  608. INT nIndex = 0;
  609. string strTemp = "";
  610. while ( str.find_first_of("\\u") != string::npos )
  611. {
  612. nIndex = str.find_first_of("\\u");
  613. strResult.append(str.substr(0, nIndex));
  614. strTemp = str.substr(nIndex + 2, 4);
  615. str = str.substr(nIndex + 2 +4);
  616. CHAR szReturn[10] = {0};
  617. union __UNION_VAR_INT{
  618. BYTE ch[2];
  619. int value;
  620. }unionVarInt;
  621. unionVarInt.ch[0] = (CharToInt(strTemp.at(2)) << 4) | (CharToInt(strTemp.at(3)) & 0x00FF);
  622. unionVarInt.ch[1] = (CharToInt(strTemp.at(0)) << 4) | (CharToInt(strTemp.at(1)) & 0x00FF);
  623. WCHAR szWide[2] = {0};
  624. szWide[0] = unionVarInt.value;
  625. UNICODE2ASCII(szWide,szReturn,10);
  626. strResult.append(szReturn);
  627. }
  628. strResult.append(str);
  629. }