UNICODE十六进制数组转成中英文

UNICODE十六进制的数组转成中英文


实现char*转换成中英文,每两个char合成一个wchar_t:

// UNICODE十六进制数组转成中英文 // hex array to wchar_t* // wchs == NULL, wchsLen as output(the size of wchs will be used) // error: return -1 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) break; tmp[1] = *phex++; // two char to a hex unsigned short hex = 0x0; hex = (tmp[0] & 0xff) << 8; hex |= tmp[1] & 0xff; // hex to wchar_t *pwchs++ = (wchar_t)hex; } return 1; }
调用实例:

//wchar_t* unicode = NULL; 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; // hex array to wchar_t* 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){ // wchar_t to CString CString tmp; wchar_t * pwch = wchs; for(int i=0; i

已实现把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的


基本测试通过,暂未发现神马问题。




你可能感兴趣的:(UNICODE十六进制数组转成中英文)