admin 管理员组文章数量: 887021
2023年12月17日发(作者:指令add)
41. // CodeConver.h
42. //
43.
44. #ifndef CodeConver_H
45. #define CodeConver_H
46.
47. namespace CodeConvert
48. {
49.
50. #include
51. #include
52. #include
53. #include
54. #include
55. #include
56.
57. // ANSI To Unicode
58. // 测试通过
59. wchar_t * ANSIToUnicode( const char* str )
60. {
61. int textlen ;
62. wchar_t * result;
63.
64. textlen = MultiByteToWideChar( CP_ACP, 0, str,-1, NULL,0 );
65. if (textlen ==0)
66. {
67. return NULL;
68. }
69. result = (wchar_t *)malloc((textlen+1)*sizeof(wchar_t));
70. memset(result,0,(textlen+1)*sizeof(wchar_t));
71. MultiByteToWideChar(CP_ACP, 0,str,-1,(LPWSTR)result,textlen );
72. return result;
73. }
74.
75. /************************************************************************/
76. // Unicode To ANSI(美国国家标准码)
77. // 测试通过
78. // [2/8/2012 liu]
79. /************************************************************************/
80. char * UnicodeToANSI( const wchar_t* str )
81. {
82. char* result;
83. int textlen;
84. textlen = WideCharToMultiByte( CP_ACP, 0, str, -1, NULL, 0, NULL, NULL );
85. if (textlen ==0)
86. {
87. return NULL;
88. }
89. result =(char *)malloc((textlen+1)*sizeof(char));
90. memset( result, 0, sizeof(char) * ( textlen + 1 ) );
91. WideCharToMultiByte( CP_ACP, 0, str, -1, result, textlen, NULL, NULL );
92. return result;
93. }
94.
95. /************************************************************************/
96. // UTF8 To Unicode
97. // [2/8/2012 liu]
98. // 测试通过
99. /************************************************************************/
100. int UTF8ToUnicode( wchar_t * &result,const char* utf8 )
101. {
102. int textlen ;
103. textlen = MultiByteToWideChar( CP_UTF8, 0, (LPCSTR)utf8,-1, NULL,0 );
104. if (textlen == 0)
105. {
106. return NULL;
107. }
108. result = (wchar_t *)malloc((textlen+1)*sizeof(wchar_t));
109. memset(result,0,textlen * 2 + 2);
110. int widelen = MultiByteToWideChar(CP_UTF8, 0,(LPCSTR)utf8,-1,(LPWSTR)result,textlen );
111. return widelen-1;
112.
113. }
114.
115. /************************************************************************/
116. // UnicodeToUTF8
117. //
118. // [2/8/2012 liu]
119. /************************************************************************/
120. char * UnicodeToUTF8( const wchar_t* str )
121. {
122. char* result;
123. int textlen;
124. textlen = WideCharToMultiByte( CP_UTF8, 0, str, -1, NULL, 0, NULL, NULL );
125. if (textlen == 0)
126. {
127. return NULL;
127. return NULL;
128. }
129. result =(char *)malloc((textlen+1)*sizeof(char));
130. memset(result, 0, sizeof(char) * ( textlen + 1 ) );
131. WideCharToMultiByte( CP_UTF8, 0, str, -1, result, textlen, NULL, NULL );
132. return result;
133. }
134. /************************************************************************/
135. // 宽字符 转换为 多字符 Unicode - ANSI
136. //
137. // [2/8/2012 liu]
138. /************************************************************************/
139. char* w2m(const wchar_t* wcs)
140. {
141. int len;
142. char* buf;
143. if (wcs =NULL)
144. {
145. return NULL;
146. }
147. len =wcstombs(NULL,wcs,0);
148. if (len == 0)
149. return NULL;
150. buf = (char *)malloc(sizeof(char)*(len+1));
151. memset(buf, 0, sizeof(char) *(len+1));
152. len =wcstombs(buf,wcs,len+1);
153. return buf;
154. }
155.
156. /************************************************************************/
157. // 多字符 转换为 宽字符 ANSI - Unicode
158. //
159. // [2/8/2012 liu]
160. /************************************************************************/
161. wchar_t* multiByte_to_wideChar(const char* mbs)
162. {
163. int len;
164. wchar_t* buf;
165. if (mbs == NULL)
166. {
167. return NULL;
168. }
169. len =mbstowcs(NULL,mbs,0);
170. if (len == 0)
171. return NULL;
172. buf = (wchar_t *)malloc(sizeof(wchar_t)*(len+1));
173. memset(buf, 0, sizeof(wchar_t) *(len+1));
174. len =mbstowcs(buf,mbs,len+1);
175. return buf;
176. }
177.
178. /************************************************************************/
179. // ANSI To UTF8
180. //
181. // [2/8/2012 liu]
182. /************************************************************************/
183. char* ANSIToUTF8(const char* str)
184. {
185. return UnicodeToUTF8(ANSIToUnicode(str));
186. }
187.
188. /************************************************************************/
189. // UTF8 To ANSI
190. // 测试通过
191. // [2/8/2012 liu]
192. /************************************************************************/
193. char* UTF8ToANSI(const char* str)
194. {
195. wchar_t * temp;
196. if (str == NULL)
197. {
198. return NULL;
199. }
200. UTF8ToUnicode(temp,str);
201. return UnicodeToANSI(temp);
202. }
203.
204.
205.
206. // 测试OK
207. // UTF8 convert to GB2312
208. void UTF8ToGB2312( const char * utf8, char * &gb2312 )
209. {
210. int nLen = MultiByteToWideChar( CP_UTF8, 0, utf8, -1, NULL, 0 );
211. if (nLen == 0)
212. {
213. gb2312 = NULL;
214. return ;
215. }
216.
217. USHORT* pwszGB2312 = new USHORT[ nLen + 1 ];
218. RtlZeroMemory( pwszGB2312, nLen * 2 + 2 );
219.
220. MultiByteToWideChar( CP_UTF8, 0, utf8, -1, pwszGB2312, nLen );
221.
222. nLen = WideCharToMultiByte( CP_ACP, 0, pwszGB2312, -1, NULL, 0, NULL, NULL );
223.
224. CHAR* pszGB2312 = new CHAR[ nLen + 1 ];
225. RtlZeroMemory( pszGB2312, nLen + 1 );
226.
227. WideCharToMultiByte( CP_ACP, 0, pwszGB2312, -1, pszGB2312, nLen, NULL, NULL );
228.
229. gb2312 = pszGB2312;
230.
231. // delete [] pszGB2312;
232. // delete [] pwszGB2312;
233. }
234.
235. // GB2312 convert to UTF8
236. // 测试通过
237. int GB2312ToUTF8( const char * gb2312,char * &utf8 )
238. {
239. int nLen1 = MultiByteToWideChar( CP_ACP, 0, gb2312, -1, NULL, 0 );
240. if (nLen1 == 0)
241. {
242. return 0;
243. }
244.
245. USHORT* pwszUTF8 = new USHORT[ nLen1 + 1 ];
246. RtlZeroMemory( pwszUTF8, nLen1 * 2 + 2 );
247.
248. MultiByteToWideChar( CP_ACP, 0, gb2312, -1, pwszUTF8, nLen1 );
249.
250. int nLen = WideCharToMultiByte( CP_UTF8, 0, pwszUTF8, -1, NULL, 0, NULL, NULL );
251.
252. CHAR* pszUTF8 = new CHAR[ nLen + 1 ];
253. RtlZeroMemory( pszUTF8, nLen + 1 );
254.
255. WideCharToMultiByte( CP_UTF8, 0, pwszUTF8, -1, pszUTF8, nLen, NULL, NULL );
256.
257. utf8 = pszUTF8;
257. utf8 = pszUTF8;
258.
259. return nLen1-1;
260.
261. // delete [] pszUTF8;
262. // delete [] pwszUTF8;
263. }
264.
265. // Gb2312 To Unicode
266. // 测试通过
267. int Gb2312ToUnicode(wchar_t * &OutUnicode,const char *gb2312)
268. {
269. char * utf8 = NULL;
270. if (gb2312==NULL)
271. {
272. OutUnicode = NULL;
273. return 0;
274. }
275. GB2312ToUTF8(gb2312,utf8);
276. int len = UTF8ToUnicode(OutUnicode,utf8);
277. return len;
278. }
279.
280. //
281.
282.
283.
284. //
285.
286. /**************************************************************************
287. *
288. * 函数名: HexToString
289. *
290. * 描 述: 16进制 转为 字符串
291. *
292. * 参 数: IN:const char *pHex,IN:unsigned long hexLen,
293. * OUT:char *pByteString
294. *
295. * 返 回: 成功:0 , 失败:1、2
296. *
297. * 作 者: liuguanglin
298. *
299. **************************************************************************/
300. DWORD HexToString(/*IN*/ const char * pHex,
344. DWORD i;
345. DWORD dwRet;
346. unsigned char * pTempBuf=NULL;
347.
348. if (pByteString==NULL || pbHex==NULL)
349. return 1;
350.
351. if(dwStrLen <= 0)
352. return 2;
353.
354. if ((dwStrLen%2)!=0)
355. {
356. pTempBuf=(BYTE*)malloc(dwStrLen+2);
357. pTempBuf[0]=0x30;
358. memcpy(&pTempBuf[1],pByteString,sizeof(BYTE)*dwStrLen);
359. dwRet=StringToHex(pTempBuf,dwStrLen+1,pbHex);
360. free(pTempBuf);
361. return dwRet;
362. }
363. else
364. {
365. for(i=0;i 366. { 367. if(i%2==0) 368. { 369. if(pByteString[i]>='0' && pByteString[i]<='9') 370. pbHex[i/2]=(pByteString[i]-0x30)<<4; 371. else if(pByteString[i]>='a' && pByteString[i]<='f') 372. pbHex[i/2]=(pByteString[i]-0x57)<<4; 373. else if(pByteString[i]>='A' && pByteString[i]<='F') 374. pbHex[i/2]=(pByteString[i]-0x37)<<4; 375. else 376. return 3; 377. } 378. else 379. { 380. if(pByteString[i]>='0' && pByteString[i]<='9') 381. pbHex[i/2]|=pByteString[i]-0x30; 382. else if(pByteString[i]>='a' && pByteString[i]<='f') 383. pbHex[i/2]|=pByteString[i]-0x57; 384. else if(pByteString[i]>='A' && pByteString[i]<='F') 385. pbHex[i/2]|=pByteString[i]-0x37; 386. else 387. return 4; 474. unsigned char *q = (unsigned char *)dst; 475. unsigned char *p = (unsigned char*) src; 476. t = q; 477. if(t == p) 478. { 479. q = (unsigned char *)malloc(count); 480. assert(q); 481. } 482. for( i=0; (size_t) i 483. if(t == p) { 484. memcpy(p, q, count); 485. free(q); 486. } 487. 488. } 489. 490. // 编码转换 491. 492. UTF8 convert to GB2312 493. VOID UTF82GB2312( /*IN*/ const char * utf8, /* OUT */ const char * gb2312 ) 494. { 495. int nLen = MultiByteToWideChar( CP_UTF8, 0, utf8 , -1, NULL, 0 ); 496. 497. USHORT* pwszGB2312 = new USHORT[ nLen + 1 ]; 498. RtlZeroMemory( pwszGB2312, nLen * 2 + 2 ); 499. 500. MultiByteToWideChar( CP_UTF8, 0, utf8 , -1, pwszGB2312, nLen ); 501. 502. nLen = WideCharToMultiByte( CP_ACP, 0, pwszGB2312, -1, NULL, 0, NULL, NULL ); 503. 504. CHAR* pszGB2312 = new CHAR[ nLen + 1 ]; 505. RtlZeroMemory( pszGB2312, nLen + 1 ); 506. 507. WideCharToMultiByte( CP_ACP, 0, pwszGB2312, -1, pszGB2312, nLen, NULL, NULL ); 508. 509. gb2312 = pszGB2312; 510. 511. delete [] pszGB2312; 512. delete [] pwszGB2312; 513. } 514. 515. GB2312 convert to UTF8 516. VOID GB23122UTF8( /*IN*/ const char * gb2312, /* OUT */ const char * utf8 ) 517. { /************************************************************************/ // UTF8 To Unicode // [2/8/2012 liu] // 测试通过 /************************************************************************/ int UTF8ToUnicode( wchar_t * &result,const char* utf8 ) { int textlen ; textlen = MultiByteToWideChar( CP_UTF8, 0, (LPCSTR)utf8,-1, NULL,0 ); if (textlen == 0) { return NULL; } result = (wchar_t *)malloc((textlen+1)*sizeof(wchar_t)); memset(result,0,textlen * 2 + 2); int widelen = MultiByteToWideChar(CP_UTF8, 0,(LPCSTR)utf8,-1,(LPWSTR)result,textlen ); return widelen-1; } /************************************************************************/ // UnicodeToUTF8 // // [2/8/2012 liu] /************************************************************************/ char * UnicodeToUTF8( const wchar_t* str ) { char* result; int textlen; textlen = WideCharToMultiByte( CP_UTF8, 0, str, -1, NULL, 0, NULL, NULL ); if (textlen == 0) { return NULL; } result =(char *)malloc((textlen+1)*sizeof(char)); memset(result, 0, sizeof(char) * ( textlen + 1 ) ); WideCharToMultiByte( CP_UTF8, 0, str, -1, result, textlen, NULL, NULL ); return result; } /************************************************************************/ // 宽字符 转换为 多字符 Unicode - ANSI // // [2/8/2012 liu] /************************************************************************/ char* w2m(const wchar_t* wcs) { int len; char* buf; if (wcs =NULL) { return NULL; } len =wcstombs(NULL,wcs,0); if (len == 0) return NULL; buf = (char *)malloc(sizeof(char)*(len+1)); memset(buf, 0, sizeof(char) *(len+1)); len =wcstombs(buf,wcs,len+1); return buf; } /************************************************************************/ // 多字符 转换为 宽字符 ANSI - Unicode // // [2/8/2012 liu] /************************************************************************/ /************************************************************************/ wchar_t* multiByte_to_wideChar(const char* mbs) { int len; wchar_t* buf; if (mbs == NULL) { return NULL; } len =mbstowcs(NULL,mbs,0); if (len == 0) return NULL; buf = (wchar_t *)malloc(sizeof(wchar_t)*(len+1)); memset(buf, 0, sizeof(wchar_t) *(len+1)); len =mbstowcs(buf,mbs,len+1); return buf; } /************************************************************************/ // ANSI To UTF8 // // [2/8/2012 liu] /************************************************************************/ char* ANSIToUTF8(const char* str) { return UnicodeToUTF8(ANSIToUnicode(str)); } /************************************************************************/ // UTF8 To ANSI // 测试通过 // [2/8/2012 liu] /************************************************************************/ char* UTF8ToANSI(const char* str) { wchar_t * temp; if (str == NULL) { return NULL; } UTF8ToUnicode(temp,str); return UnicodeToANSI(temp); } // 测试OK // UTF8 convert to GB2312 void UTF8ToGB2312( const char * utf8, char * &gb2312 ) { int nLen = MultiByteToWideChar( CP_UTF8, 0, utf8, -1, NULL, 0 ); if (nLen == 0) { gb2312 = NULL; return ; } USHORT* pwszGB2312 = new USHORT[ nLen + 1 ]; RtlZeroMemory( pwszGB2312, nLen * 2 + 2 ); MultiByteToWideChar( CP_UTF8, 0, utf8, -1, pwszGB2312, nLen ); nLen = WideCharToMultiByte( CP_ACP, 0, pwszGB2312, -1, NULL, 0, NULL, NULL ); CHAR* pszGB2312 = new CHAR[ nLen + 1 ]; RtlZeroMemory( pszGB2312, nLen + 1 ); RtlZeroMemory( pszGB2312, nLen + 1 ); WideCharToMultiByte( CP_ACP, 0, pwszGB2312, -1, pszGB2312, nLen, NULL, NULL ); gb2312 = pszGB2312; // delete [] pszGB2312; // delete [] pwszGB2312; } // GB2312 convert to UTF8 // 测试通过 int GB2312ToUTF8( const char * gb2312,char * &utf8 ) { int nLen1 = MultiByteToWideChar( CP_ACP, 0, gb2312, -1, NULL, 0 ); if (nLen1 == 0) { return 0; } USHORT* pwszUTF8 = new USHORT[ nLen1 + 1 ]; RtlZeroMemory( pwszUTF8, nLen1 * 2 + 2 ); MultiByteToWideChar( CP_ACP, 0, gb2312, -1, pwszUTF8, nLen1 ); int nLen = WideCharToMultiByte( CP_UTF8, 0, pwszUTF8, -1, NULL, 0, NULL, NULL ); CHAR* pszUTF8 = new CHAR[ nLen + 1 ]; RtlZeroMemory( pszUTF8, nLen + 1 ); WideCharToMultiByte( CP_UTF8, 0, pwszUTF8, -1, pszUTF8, nLen, NULL, NULL ); utf8 = pszUTF8; return nLen1-1; // delete [] pszUTF8; // delete [] pwszUTF8; } // Gb2312 To Unicode // 测试通过 int Gb2312ToUnicode(wchar_t * &OutUnicode,const char *gb2312) { char * utf8 = NULL; if (gb2312==NULL) { OutUnicode = NULL; return 0; } GB2312ToUTF8(gb2312,utf8); int len = UTF8ToUnicode(OutUnicode,utf8); return len; } // // /************************************************************************** * * 函数名: HexToString * * 描 述: 16进制 转为 字符串
版权声明:本文标题:字符编码转换_进制转换(GB2312,GBK,JNI,HexTOStr) 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/free/1702810881h431772.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论