Wchar_t and char Conversion

Source: Internet
Author: User

Address: http://hi.baidu.com/gchrist/blog/item/29a138f533383e24bd310934.html

 

Environment: Dev cpp platform i386

In the C ++ standard, wchar_t is a wide character type. Each wchar_t type occupies 2 bytes and has a 16-bit width. Wchar_t is required for the representation of Chinese characters. Char, we all know, occupies a byte, 8-bit width. As a matter of fact, the conversion between wchar_t and char is not hard to achieve.

The code for converting wchar_t to char is as follows:

The following wchar_t and char variables are available:

Wchar_t w_cn = '中 ';
Char c_cn [2] = {'0 '};

Char * C2W (wchar_t w_cn, char c_cn [2])

{

// Following code convert wchar to char
C_cn [0] = w_cn> 8;
C_cn [1] = w_cn;
C_cn [2] = '/0 ';

Return c_cn;

}

Note that a 16-bit wchar_t needs to be stored with two 8-bit char. We can find another problem: the high byte of wchar_t should be stored in the low byte of the char array. (I think it's strange that I have not studied it carefully ).

This completes the conversion from wchar_t to char. The conversion from char to wchar_t is similar.

In C ++, If You Want To: cout <w_cn <endl; print wchar_t, that won't work. Why? I think the wchar_t type is not overloaded <operator. To display, my method is
String cn (c_cn );
Cout <cn <endl;

In this way, wchar_t characters can be correctly printed. Does it seem like I have done many things, but what I have done is simple? Well, I have the same feeling, but there is no way. The simpler method is, of course, to directly use the API (I am a lazy person looking for it, and I am a self-help engineer who can see something more clearly ), however, such conversions are more flexible and should be used in practice.
With the above foundation, the conversion code from the wchar_t string to the char string is given below:

Char * W2C (const wchar_t * pw, char * pc)
{
// Cout <* pw <endl; // The result cannot be correctly displayed.
* Pc ++ = * pw> 8;
* Pc = * pw;
Return 0;
}

Char * wstr2cstr (const wchar_t * pwstr, char * pcstr, size_t len)
{
Char * ptemp = pcstr;
If (pwstr! = NULL & pcstr! = NULL)
{

Size_t wstr_len = wcslen (pwstr );
Len = (len> wstr_len )? Wstr_len: len;
While (len --> 0)
{

W2C (pwstr, pcstr );
Pwstr ++;
Pcstr + = 2; // It is the same as what we started to say + 2, not + 1
}
* Pcstr = '/0 ';
Return ptemp;
}
Return 0;
}

The above is the code, test:

Int main (int arg, char * argv [])

{

Wchar_t pwstr [] = {'my', 'yes', 'zhong', 'state', 'people '};

Char * pcstr = (char *) new char [2 * wcslen (pwstr) + 1];
Memset (pcstr, 0, 2 * wcslen (pwstr) + 1 );
Wstr2cstr (pwstr, pcstr, wcslen (pwstr ));

Str. assign (pcstr );
Cout <str <endl;
Delete [] pcstr;

}

Over!

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.