生活随笔
收集整理的這篇文章主要介紹了
UNICODE十六进制数组转成中英文
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
UNICODE十六進制的數組轉成中英文
實現char*轉換成中英文,每兩個char合成一個wchar_t:
[cpp]view plaincopy
?? ?? ?? ?? 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;?? }??
調用實例:
[cpp]view plaincopy
<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的
基本測試通過,暫未發現神馬問題。
總結
以上是生活随笔為你收集整理的UNICODE十六进制数组转成中英文的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。