UNICODE十六进制的数组转成中英文
实现char*转换成中英文,每两个char合成一个wchar_t:
-
-
-
-
- int YCodec::HexArray2WChars(char* hexarry, int hexarrylen, wchar_t* wchs, int* wchslen)
- {
- if(hexarry == NULL || hexarrylen<=0) return -1;
- if(wchs == NULL){
- *wchslen = hexarrylen/2 + (hexarrylen%2==0 ? 0 : 1);
- return 1;
- }
- memset(wchs, 0, *wchslen * sizeof(wchar_t));
- char tmp[3];
- char* phex = hexarry;
- wchar_t* pwchs = wchs;
- for(int i=0; i<hexarrylen; i++){
- memset(tmp, 0, 3);
- tmp[0] = *phex++;
- if(++i >= hexarrylen) break;
- tmp[1] = *phex++;
-
- unsigned short hex = 0x0;
- hex = (tmp[0] & 0xff) << 8;
- hex |= tmp[1] & 0xff;
-
- *pwchs++ = (wchar_t)hex;
- }
-
- return 1;
- }
调用实例:
- <span style="white-space:pre"> </span>
- char ascii[] = {0x62, 0x4b, 0x67, 0x3a,
- 0x53, 0xf7, 0x4e, 0x3a,
- 0x00, 0x31, 0x00, 0x38,
- 0x00, 0x38, 0x00, 0x38,
- 0x00, 0x37, 0x00, 0x36,
- 0x00, 0x35, 0x00, 0x35,
- 0x00, 0x32, 0x00, 0x31,
- 0x00, 0x30, 0x76, 0x84
- };
- int wchslen = 0;
- YCodec yCodec;
-
- if(yCodec.HexArray2WChars(ascii, 32, NULL, &wchslen) != -1){
- wchar_t* wchs = new wchar_t[wchslen+1];
- memset(wchs, 0, sizeof(wchar_t)*(wchslen+1) );
- int n = yCodec.HexArray2WChars(ascii, 32, wchs, &wchslen);
- if(n != -1){
-
- CString tmp;
- wchar_t * pwch = wchs;
- for(int i=0; i<wchslen; i++) tmp.AppendChar(*pwch++);
- m_Disp.SetWindowTextW(tmp);
- }
-
- delete []wchs;
- }
在MFC程序中测试通过。
已实现把0x62, 0x4b, 0x67, 0x3a, 0x53, 0xf7, 0x4e, 0x3a, 0x00, 0x31, 0x00, 0x38, 0x00, 0x38, 0x00, 0x38, 0x00, 0x37, 0x00, 0x36, 0x00, 0x35, 0x00, 0x35, 0x00, 0x32, 0x00, 0x31, 0x00, 0x30, 0x76, 0x84这一串unicode的十六进制字符数组转成中英文。
结果为:
手机号为18887655210的
基本测试通过,暂未发现神马问题。