C ++ implements conversion between Chinese and English Chinese and Unicode hexadecimal strings

Source: Internet
Author: User
Tags hex code

I have previously done this conversion between Chinese and English and Unicode hexadecimal strings, but I found it was a small problem. Now I have another one. No problems have been found in the test. Record it first.

I tried to use multibytetowidechar and widechartomultibyte to implement this function, but I found it was not feasible. That is, it cannot be implemented as follows:

4e2d65870045006e0067006c006900730068

And

4e2d65870045006e0067006c006900730068 --> Chinese English

For such a conversion, all the above are strings.

See the following source code: ycodec is a class written by myself.

Ycodec. H source code:

# Include <iostream> using namespace STD; Class ycodec {public: ycodec ();~ Ycodec (); // Char to hex codeint char2hex (char ch); // hex to charchar hex2char (unsigned int N); // num ^ indexlong mindex (INT num, int index); // string to hex codelong string2hex (char * string, int strlen); // hex to char * int hex2string (long hex, char * string, int * slen); // hex to wchar_t codewchar_t hex2wchart (long HEX); // convert Unicode hexadecimal string to Chinese and English // hex string to wchar_t * int hexstr2wchars (char * hexstr, int hexstrlen, int eachchar, wchar_t * wchs, int * wchslen); // wchar_t to hex codelong wchart2hex (wchar_t wch ); // convert Chinese and English to Unicode hexadecimal string // wchar_t * To char * (hex string) int wchart2hexstr (wchar_t * wchs, int wchslen, int eachchar, char * hexstr, int * hexstrlen );};

Source code of the ycodec. cpp file:

# Include "stdafx. H "# include" ycodec. H "# include <stdio. h> # include <stdlib. h> # include <string. h> ycodec: ycodec () {} ycodec ::~ Ycodec () {}// Char to hex code // error: Return-1 int ycodec: char2hex (char ch) {int n =-1; Switch (CH) {Case '0': n = 0; break; Case '1': n = 1; break; Case '2': N = 2; break; Case '3 ': n = 3; break; Case '4': N = 4; break; Case '5': n = 5; break; Case '6': n = 6; break; case '7': n = 7; break; Case '8': n = 8; break; Case '9': n = 9; break; Case 'A ': case 'A': n = 10; break; Case 'B': n = 11; break; Case 'C ': n = 12; bre AK; Case 'D': n = 13; break; Case 'E': n = 14; break; Case 'F ': case 'F': n = 15; break; default: break;} return N;} // hex to Char/error: Return-1 char ycodec :: hex2char (unsigned int N) {char ch; If (n> = 0 & n <= 9) CH = 48 + N; else if (n> = 10 & n <= 15) CH = 65-10 + N; else CH =-1; return ch;} // num ^ indexlong ycodec :: mindex (INT num, int index) {long S = 1; int I = 0; while (I <index) {S * = num; I ++;} return S;} // string to hex code // error: Return-1 long ycodec: string2hex (char * string, int strlen) {long hex =-1; int I = 0, n = 0; char * P = string; P + = strlen-1; if (string = NULL) return hex; if (strlen <= 0 | strlen> 10) return hex; hex = 0; do {n = char2hex (* p --); hex + = N * mindex (16, I ++);} while (I <strlen); Return hex;} // hex to char * // string = NULL, slen = the size of string (slen as output) // string! = NULL, input the length of string // error: Return-1int ycodec: hex2string (long hex, char * string, int * slen) {char TMP [11] = {0}; If (hex <0) Return-1; if (string = NULL) {// count the length it will be usedsprintf (TMP, "% x", Hex); * slen = strlen (TMP); return 1;} memset (string, 0, * slen); sprintf (string, "% x", Hex); return 1;} // hex to wchar_t code // eg: Input 0x5e74, return year // error: return-1wchar_t ycod EC: hex2wchart (long HEX) {wchar_t wch =-1; if (hex <0) return wch; wch = (wchar_t) hex; return wch ;} // hex string to wchar_t * // convert the Unicode hexadecimal string to both Chinese and English. // convert each eachchar to a wchar_t // wchs = NULL, wchslen as output (the size of wchs will be used) // error: Return-1int ycodec: hexstr2wchars (char * hexstr, int hexstrlen, int eachchar, wchar_t * wchs, int * wchslen) {If (hexstr = NULL | hexstrlen <= 0 | eachchar <= 0) Return-1; if (wchs = NULL) {// count the size wchs it will be used * wchslen = hexstrlen/eachchar + (hexstrlen % eachchar> 0? 1: 0); return 1;} memset (wchs, 0, * wchslen * sizeof (wchar_t); char * TMP = new char [eachchar + 1]; char * P = hexstr; wchar_t * pwch = wchs; For (INT I = 0; I 

Conversion of Chinese and English to Unicode hexadecimal strings:

CString input;m_Input.GetWindowTextW(input);CString tmp;YCodec yCodec;// CString to wchar_t*int len = input.GetLength();wchar_t* wchs = new wchar_t[len+1];memset(wchs, 0, sizeof(wchar_t)*(len+1));wcscpy(wchs, input.GetBuffer(len) );// wchar_t* to hex stringint hexlen = 0;if(yCodec.Wchart2HexStr(wchs, len, 4, NULL, &hexlen) != -1){char* hexstr = new char[hexlen+1];memset(hexstr, 0, hexlen+1);int n = yCodec.Wchart2HexStr(wchs, len, 4, hexstr, &hexlen);if(n != -1){// char* to CStringchar* p = hexstr;for(int i=0; i

This function was tested in the MFC program. You can achieve the following results:

The input string "文english English" 4e2d65870045006e0067006c006900730068 "is converted into four char data types for each Chinese or English character.

Example of converting Unicode hexadecimal string to Chinese and English:

CString input;m_Input.GetWindowTextW(input);CString tmp;YCodec yCodec;// CString to char*int strlen = input.GetLength();char* str = new char[strlen+1];memset(str, 0, strlen+1);for(int i=0; i<strlen; i++)str[i] = input.GetAt(i);// hex string to wchar_t*int wchslen = 0;if(yCodec.HexStr2WChars(str, strlen, 4, NULL, &wchslen) != -1){wchar_t* wchs = new wchar_t[wchslen+1];memset(wchs, 0, sizeof(wchar_t)*(wchslen+1));int n = yCodec.HexStr2WChars(str, strlen, 4, wchs, &wchslen);if(n != -1){// wchar_t to CStringwchar_t* pwch = wchs;for(int i=0; i<wchslen; i++)tmp.AppendChar(*pwch++);}if(wchs)delete []wchs;}m_Disp.SetWindowTextW(tmp);

The input string "4e2d65870045006e0067006c006900730068" is converted to a "Chinese English" string, and each four char data types are converted to one Chinese character or English character.

The above code is tested and feasible, and no problems are found for the time being.

Test Programs and source code: (do not download them separately)

Http://download.csdn.net/detail/brantyou/4241745

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.