UNICode 16進数配列を中国語と英語に変換


ユニコード16進数の配列を中国語と英語に変換
char*を中国語と英語に変換し、2つの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プログラムでテストに合格した.
0 x 62,0 x 4 b,0 x 67,0 x 3 a,0 x 53,0 xf 7,0 x 4 e,0 x 3 a,0 x 31,0 x 00,0 x 38,0 x 00,0 x 00,0 x 38,0 x 00,0 x 00,0 x 37,0 x 36,0 x 00,0 x 00,0 x 35,0 x 00,0 x 32,0 x 0,0 x 31,0 x 0,0 x 0,0 x 30,0 x 76,0 x 84の一連のunicodeの16進数文字配列を中国語と英語に変換することが実現された.
結果:
携帯電話番号18887655210の
基本テストに合格し、神馬問題は発見されなかった.