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

13 篇文章 0 订阅

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; i++){
		memset(tmp, 0, 3);
		tmp[0] = *phex++;
		if(++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<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的


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




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值