Char to wchar_t and wchar_t to Char

Source: Internet
Author: User

Using the WideCharToMultiByte function to convert, the function maps a Unicode string to a multibyte string. Usually suitable for use on window platforms.

#include <tchar.h>#include<windows.h>int_tmain (intARGC, _tchar*argv[]) {wchar_t pwstr[]=l"I am a Chinese"; wchar_t pwstr2[ -]; Char*pcstr = (Char*)malloc(sizeof(Char)*(2* Wcslen (PWSTR) +1)); memset (Pcstr,0,2* Wcslen (PWSTR) +1 ); W2C (Pcstr,pwstr,2* Wcslen (PWSTR) +1) ; printf ("%s\n", PCSTR); c2w (PWSTR2, -, PCSTR); wprintf (L"%s", PWSTR2);  Free(PCSTR);return 0;}//the implementation function of turning wchar_t* into char* is as follows:Char*W2C (Char*PCSTR,Constwchar_t *pwstr, size_t len) {intNlength=wcslen (PWSTR);//get the converted lengthintNbytes = WideCharToMultiByte (0,//Specify the code page used to perform the conversion0,//no special flags to handle unmapped charactersPwstr,//wide character string to convertNlength,//The number of wide characters in that stringNULL,//no output buffer given, we just want to know how long it needs to be0, NULL,//no replacement character givenNULL); //we don ' t want to know if a character didn ' t make it through the translation//Make sure the buffer was big enough for this, making it larger if necessaryif(Nbytes>len) nbytes=Len;//convert Unicode characters to ASCII characters with the results obtained aboveWideCharToMultiByte (0,//Specify the code page used to perform the conversion0,//no special flags to handle unmapped charactersPwstr,//wide character string to convertNlength,//The number of wide characters in that stringPcstr,//put the output ASCII characters at the end of the bufferNbytes,//There is at least this much space thereNULL,//no replacement character givenNULL);returnpcstr;}//the implementation function of turning char* into wchar_t* is as follows://This is the same principle that converts Asii characters to Unicode charactersvoidc2w (wchar_t *pwstr,size_t Len,Const Char*str) {if(str) {size_t nu=strlen (str); size_t N= (size_t) MultiByteToWideChar (CP_ACP,0,(Const Char*) STR, (int) Nu,NULL,0); if(N>=len) n=len-1; MultiByteToWideChar (CP_ACP,0,(Const Char*) STR, (int) Nu,pwstr, (int) n); Pwstr[n]=0; }} or better with this method:============I've done it myself.//convert ASCII characters to Unicode characterswchar_t* Cphone_hq::ctow (wchar_t *pwstr,Const Char*str) {wchar_t*buffer;if(str) {size_t nu=strlen (str); size_t N= (size_t) MultiByteToWideChar (CP_ACP,0,(Const Char*) STR,int(NU), NULL,0); Buffer=0; Buffer=Newwchar_t[n+1]; //if (N>=len) n=len-1;:: MultiByteToWideChar (CP_ACP,0,(Const Char*) STR,int(NU), buffer,int(n)); }returnbuffer;Deletebuffer;}

Relevant knowledge points:

The advent of Unicode is to meet the needs of software internationalization. Unicode differs from double-byte character set (DBCS).

First, the related operation function

1. DBCS uses the following function to manipulate the string:

charnext--after a character is obtained

charprev--get the previous character

isdbcsleadbyte--determines whether the first byte of a two-byte character

The C + + run-time library provides a series of functions that begin with "_mbs" to manipulate DBCS. Similar functions are _mbscat and so on.

2. The ANSI character set is an American standard. The C + + Runtime library provides functions for some columns beginning with "STR" to manipulate this character set.

3. The C + + Runtime library provides a series of functions that begin with "WCS" for the Unicode character set.

Second, the corresponding data type

1, the ANSI character is defined as char.

2. The Unicode character is defined as wchar_t.

Third, the use of the environment

1, the first thing to note is that Win98 support for Unicode is very weak, so if you want to run Unicode-compiled programs on Win98, it can cause run-time errors or failures.

2, because the kernel of Win2000 and later OS is written in Unicode, so although it is possible to run an ANSI-coded program on it, many places in its operation need to convert ANSI to Unicode, then call the Unicode version of the function, Because the process of this conversion exists, the ANSI program runs inefficiently. It is best to use Unicode to write programs on Win2000.

Iv. Preparation of common procedures

1. When programming, use the TCHAR data type, which can be converted to ANSI or Unicode based on the definition of a precompiled macro.

2. Precompiled macro _mbcs, _unicode, and Unicode. _MBCS is a compilation macro for multibyte and ANSI strings. The TCHAR is converted to char at this time. _unicode and Unicode are Unicode-encoded precompiled macros, and TCHAR will be converted to wchar_t.

3, _unicode and Unicode and _MBCS cannot be defined at the time of compilation.

4, _UNICODE macro for the C run-time library header files, Unicode macros for Windows header files. These two macros are generally defined at the same time.

V. Conversion functions

1, Unicode conversion to ANSI use: MultiByteToWideChar.

2, ANSI conversion to Unicode use: WideCharToMultiByte.

Six, sizeof strlen wcslen Tcslen comparison

sizeof: Gets the byte length of the string, which contains '/0 '.

Strlen: Gets the number of characters in a multibyte string that does not contain '/0 '.

Wcslen: Gets the number of characters in a wide-byte string that does not contain '/0 '.

Tcslen: Gets the length of the character in a wide-byte/multibyte string and does not contain '/0 '.

--------------------------------------------------------------------------------------------------------------- -----------------------------------

Wide character to multiple characters:

size_t wcstombs (char *mbstr, const wchar_t *WCSTR, size_t count);

Multi-character to-width characters:

size_t mbstowcs (wchar_t *wcstr, const char *MBSTR, size_t count);

Another: L "AB" is a C/s + + standard macro, use is no problem

1, some function interfaces in the client need Unicode, these resources are also local, you can directly use MultiByteToWideChar or Mbstowcs+setlocale conversion

2, for the need from the Chinese client-> server-to-Korean client, in the case of text, you need to text language code together, in the receiver can use the specified code, conversion. The server can also use this code conversion if necessary, so that it can display multiple languages simultaneously on the client.

original link:char to wchar_t and wchar_t to Char

Char to wchar_t and wchar_t to Char

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.