CharacterConvert.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. #include "stdafx.h"
  2. #include "CharacterConvert.h"
  3. //////////////////////////////////////////////////////////////////////////
  4. /************************************************************************/
  5. /*
  6. 函数: utf82unicode
  7. 描述: utf8字符串转为unicode字符串;
  8. 参数:
  9. pszutf8 utf8字符串;
  10. 返回: unicode字符串;
  11. 注意: 返回的指针所指向的内存需要释放;
  12. */
  13. /************************************************************************/
  14. WCHAR* utf82unicode(__in const char *pszutf8)
  15. {
  16. int wnSize = MultiByteToWideChar(CP_UTF8, 0, pszutf8, -1, NULL, 0);
  17. if (wnSize == ERROR_NO_UNICODE_TRANSLATION)
  18. {
  19. throw std::exception("Invalide UTF-8 sequence");
  20. return NULL;
  21. }
  22. if ( wnSize == 0 )
  23. {
  24. throw std::exception("Error in conversion");
  25. return NULL;
  26. }
  27. WCHAR *pwResult = new WCHAR[wnSize];
  28. int nConvertSize = MultiByteToWideChar(CP_UTF8, 0, pszutf8, -1, pwResult,wnSize);
  29. if ( nConvertSize != wnSize )
  30. {
  31. throw std::exception("la falla");
  32. if(pwResult)
  33. delete pwResult;
  34. return NULL;
  35. }
  36. return pwResult;
  37. }
  38. /************************************************************************/
  39. /*
  40. 函数: utf82unicode
  41. 描述: utf8字符串转为unicode字符串;
  42. 参数:
  43. pszutf8 utf8字符串;
  44. pszunicode 返回的unicode字符串;
  45. 返回: unicode字符串大小;
  46. */
  47. /************************************************************************/
  48. int utf82unicode(__in const char *pszutf8,__inout WCHAR *pszunicode)
  49. {
  50. int wnSize = MultiByteToWideChar(CP_UTF8, 0, pszutf8, -1, NULL, 0);
  51. if (wnSize == ERROR_NO_UNICODE_TRANSLATION)
  52. {
  53. throw std::exception("Invalide UTF-8 sequence");
  54. //return 0;
  55. }
  56. if ( wnSize == 0 )
  57. {
  58. throw std::exception("Error in conversion");
  59. //return 0;
  60. }
  61. int nConvertSize = MultiByteToWideChar(CP_UTF8, 0, pszutf8, -1, pszunicode, wnSize);
  62. if ( nConvertSize != wnSize )
  63. {
  64. throw std::exception("la falla");
  65. //return 0;
  66. }
  67. return wnSize;
  68. }
  69. /************************************************************************/
  70. /*
  71. 函数: unicode2acsii
  72. 描述: unicode字符串转为acsii字符串;
  73. 参数:
  74. pszunicode unicode字符串;
  75. 返回: acsii字符串;
  76. 注意: 返回的指针所指向的内存需要释放;
  77. */
  78. /************************************************************************/
  79. CHAR* unicode2acsii(__in const WCHAR *pszunicode)
  80. {
  81. int asciisize = WideCharToMultiByte(CP_OEMCP, 0, pszunicode, -1, NULL, 0, NULL, NULL);
  82. if (asciisize == ERROR_NO_UNICODE_TRANSLATION)
  83. {
  84. throw std::exception("Invalid UTF-8 sequence.");
  85. return NULL;
  86. }
  87. if (asciisize == 0)
  88. {
  89. throw std::exception("Error in conversion.");
  90. return NULL;
  91. }
  92. CHAR *pAscii = new CHAR[asciisize];
  93. int convresult = WideCharToMultiByte(CP_OEMCP, 0, pszunicode, -1, pAscii, asciisize, NULL, NULL);
  94. if (convresult != asciisize)
  95. {
  96. throw std::exception("La falla!");
  97. if(pAscii) delete pAscii;
  98. return NULL;
  99. }
  100. return pAscii;
  101. }
  102. /************************************************************************/
  103. /*
  104. 函数: unicode2acsii
  105. 描述: unicode字符串转为acsii字符串;
  106. 参数:
  107. pszunicode unicode字符串;
  108. pszacsii 返回的acsii字符串;
  109. 返回: acsii字符串大小;
  110. */
  111. /************************************************************************/
  112. int unicode2acsii(__in const WCHAR *pszunicode,__inout CHAR *pszacsii)
  113. {
  114. int asciisize = WideCharToMultiByte(CP_OEMCP, 0, pszunicode, -1, NULL, 0, NULL, NULL);
  115. if (asciisize == ERROR_NO_UNICODE_TRANSLATION)
  116. {
  117. throw std::exception("Invalid UTF-8 sequence.");
  118. }
  119. if (asciisize == 0)
  120. {
  121. throw std::exception("Error in conversion.");
  122. }
  123. int convresult = WideCharToMultiByte(CP_OEMCP, 0, pszunicode, -1, pszacsii, asciisize, NULL, NULL);
  124. if (convresult != asciisize)
  125. {
  126. throw std::exception("La falla!");
  127. }
  128. return asciisize;
  129. }
  130. /************************************************************************/
  131. /*
  132. 函数: utf82ascii
  133. 描述: 将utf8字符串转为ascii字符串;
  134. 参数:
  135. pszutf8 utf8字符串;
  136. 返回: ascii字符串;
  137. 注意: 返回的指针需要手动释放所指内存;
  138. */
  139. /************************************************************************/
  140. CHAR* utf82ascii(const CHAR *pszutf8)
  141. {
  142. // 先把 utf8 转为 unicode ;
  143. WCHAR *pwstr = utf82unicode(pszutf8);
  144. // 最后把 unicode 转为 ascii ;
  145. CHAR *pacsii = NULL;
  146. if( pwstr )
  147. pacsii = unicode2acsii(pwstr);
  148. if (pwstr)
  149. delete pwstr;
  150. return pacsii;
  151. }
  152. /************************************************************************/
  153. /*
  154. 函数: utf82ascii
  155. 描述: 将utf8字符串转为ascii字符串;
  156. 参数:
  157. pszutf8 utf8字符串;
  158. 返回: ascii字符串;
  159. 注意: 返回的指针需要手动释放所指内存;
  160. */
  161. /************************************************************************/
  162. int utf82ascii(__in const CHAR *pszutf8, __inout CHAR* pszacsii)
  163. {
  164. // 先把 utf8 转为 unicode ;
  165. WCHAR *pwstr = utf82unicode(pszutf8);
  166. // 最后把 unicode 转为 ascii ;
  167. int nascii = 0;
  168. if( pwstr )
  169. nascii = unicode2acsii(pwstr,pszacsii);
  170. if (pwstr)
  171. delete pwstr;
  172. return nascii;
  173. }
  174. /************************************************************************/
  175. /*
  176. 函数: unicode2uft8
  177. 描述: 将unicode字符串转为utf8字符串;
  178. 参数:
  179. pszunicode unicode字符串;
  180. 返回: utf8字符串;
  181. 注意: 返回的指针需要手动释放所指内存;
  182. */
  183. /************************************************************************/
  184. CHAR* unicode2uft8(__in const WCHAR *pszunicode)
  185. {
  186. int utf8size = WideCharToMultiByte(CP_UTF8, 0, pszunicode, -1, NULL, 0, NULL, NULL);
  187. if (utf8size == 0)
  188. {
  189. throw std::exception("Error in conversion.");
  190. return NULL;
  191. }
  192. CHAR* putf8 = new CHAR[utf8size];
  193. int convresult = WideCharToMultiByte(CP_UTF8, 0, pszunicode, -1, putf8, utf8size, NULL, NULL);
  194. if (convresult != utf8size)
  195. {
  196. throw std::exception("La falla!");
  197. if(putf8)delete putf8;
  198. return NULL;
  199. }
  200. return putf8;
  201. }
  202. /************************************************************************/
  203. /*
  204. 函数: unicode2uft8
  205. 描述: 将unicode字符串转为utf8字符串;
  206. 参数:
  207. pszunicode unicode字符串;
  208. pszutf8 返回的utf8字符串;
  209. 返回: utf8字符串大小;
  210. */
  211. /************************************************************************/
  212. int unicode2uft8(__in const WCHAR *pszunicode,__inout CHAR* pszutf8)
  213. {
  214. int utf8size = WideCharToMultiByte(CP_UTF8, 0, pszunicode, -1, NULL, 0, NULL, NULL);
  215. if (utf8size == 0)
  216. {
  217. throw std::exception("Error in conversion.");
  218. }
  219. int convresult = WideCharToMultiByte(CP_UTF8, 0, pszunicode, -1, pszutf8, utf8size, NULL, NULL);
  220. if (convresult != utf8size)
  221. {
  222. throw std::exception("La falla!");
  223. }
  224. return utf8size;
  225. }
  226. /************************************************************************/
  227. /*
  228. 函数: ascii2unicode
  229. 描述: 将ascii字符串转为unicode字符串;
  230. 参数:
  231. pszascii ascii字符串;
  232. 返回: unicode字符串;
  233. 注意: 返回的指针需要手动释放其所指的内存;
  234. */
  235. /************************************************************************/
  236. WCHAR* ascii2unicode(__in const CHAR* pszascii)
  237. {
  238. int wSize = MultiByteToWideChar (CP_ACP, 0, pszascii, -1, NULL, 0);
  239. if (wSize == ERROR_NO_UNICODE_TRANSLATION)
  240. {
  241. throw std::exception("Invalid UTF-8 sequence.");
  242. return NULL;
  243. }
  244. if (wSize == 0)
  245. {
  246. throw std::exception("Error in conversion.");
  247. return NULL;
  248. }
  249. WCHAR *punicode = new WCHAR[wSize];
  250. int convresult = MultiByteToWideChar (CP_ACP, 0, pszascii, -1, punicode, wSize);
  251. if (convresult != wSize)
  252. {
  253. throw std::exception("La falla!");
  254. if(punicode) delete punicode;
  255. return NULL;
  256. }
  257. return punicode;
  258. }
  259. /************************************************************************/
  260. /*
  261. 函数: ascii2unicode
  262. 描述: 将ascii字符串转为unicode字符串;
  263. 参数:
  264. pszascii ascii字符串;
  265. 返回: unicode字符串;
  266. 注意: 返回的指针需要手动释放其所指的内存;
  267. */
  268. /************************************************************************/
  269. int ascii2unicode(__in const CHAR* pszascii,__inout WCHAR *pszunicode)
  270. {
  271. int wSize = MultiByteToWideChar (CP_ACP, 0, pszascii, -1, NULL, 0);
  272. if (wSize == ERROR_NO_UNICODE_TRANSLATION)
  273. {
  274. throw std::exception("Invalid UTF-8 sequence.");
  275. }
  276. if (wSize == 0)
  277. {
  278. throw std::exception("Error in conversion.");
  279. }
  280. int convresult = MultiByteToWideChar (CP_ACP, 0, pszascii, -1, pszunicode, wSize);
  281. if (convresult != wSize)
  282. {
  283. throw std::exception("La falla!");
  284. }
  285. return wSize;
  286. }
  287. /************************************************************************/
  288. /*
  289. 函数: ascii2utf8
  290. 描述: 将ascii字符串转为utf8字符串;
  291. 参数:
  292. pszascii ascii字符串;
  293. 返回: uft8字符串;
  294. 注意: 返回的指针需要手动释放其所指的内存;
  295. */
  296. /************************************************************************/
  297. CHAR* ascii2utf8(__in const CHAR* pszascii)
  298. {
  299. // 先把 ascii 转为 unicode ;
  300. WCHAR *pwstr = ascii2unicode(pszascii);
  301. // 最后把 unicode 转为 utf8 ;
  302. CHAR* putf8 = NULL;
  303. if (pwstr)
  304. putf8 = unicode2uft8(pwstr);
  305. if(pwstr)
  306. delete pwstr;
  307. return putf8;
  308. }
  309. /************************************************************************/
  310. /*
  311. 函数: ascii2utf8
  312. 描述: 将ascii字符串转为utf8字符串;
  313. 参数:
  314. pszascii ascii字符串;
  315. 返回: uft8字符串;
  316. 注意: 返回的指针需要手动释放其所指的内存;
  317. */
  318. /************************************************************************/
  319. int ascii2utf8(__in const CHAR* pszascii,__inout CHAR* pszutf8)
  320. {
  321. // 先把 ascii 转为 unicode ;
  322. WCHAR *pwstr = ascii2unicode(pszascii);
  323. // 最后把 unicode 转为 utf8 ;
  324. int nSize = 0;
  325. if (pwstr)
  326. nSize = unicode2uft8(pwstr,pszutf8);
  327. if(pwstr)
  328. delete pwstr;
  329. return nSize;
  330. }
  331. //wchar* to char*
  332. int WChar2Char(__inout char* pDest, __in const wchar_t* pSource)
  333. {
  334. if(pSource == NULL || pDest == NULL)
  335. return -1;
  336. int nLen = ::WideCharToMultiByte(CP_ACP, NULL, pSource, wcslen(pSource), NULL, 0, NULL, NULL);
  337. // Unicode版对应的strlen是wcslen
  338. ::WideCharToMultiByte(CP_ACP, NULL, pSource, wcslen(pSource), pDest, nLen, NULL, NULL);
  339. // 最后加上'\0'
  340. pDest[nLen] = '\0';
  341. return nLen;
  342. }
  343. //char* to wchar*
  344. int Char2WChar(__inout wchar_t* pDest, __in const char* pSource)
  345. {
  346. if(pSource == NULL || pDest == NULL)
  347. return -1;
  348. int nLen = ::MultiByteToWideChar(CP_ACP, 0, pSource, -1, NULL, 0);
  349. ::MultiByteToWideChar(CP_ACP, 0, pSource, -1, pDest, nLen);
  350. return nLen;
  351. }