EncodingConversion.cpp 17 KB

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