Widechartomultibyte and multibytetowidechar Functions

Source: Internet
Author: User

Http://hi.baidu.com/magicfuzzx/item/e5fd6a954c0a3aae82d29531

// Convert a unicode string to a multi-byte string

Int widechartomultibyte (

Uint codePage, // code page, usually cp_acp

DWORD dwflags, // additional mark, usually 0

Lpcwstr lpwidecharstr, // Unicode string

Int cchwidechar, // Unicode string size, (-1) determined by the function

Lpstr lpmultibytestr, // buffer of ANSI string, size must be specified

Int cbmultibyte, // the size of the ANSI string buffer (0)

Lpstr lpdefaultchar, // It is generally null. When conversion is not allowed, use the parameter to change the specified character.

Lpbool lpuseddefaultchar // If the conversion is successful, the value is false. If the conversion fails, the value is true.

);

Cbmultibytestr: If this parameter is set to 0, the function is used to return the required buffer size, and the preceding lpmutlibytestr parameter can be null (because it has lost its meaning ).

CodePage: the code page associated with the newly converted string.

Dwflags: allows us to specify additional conversion control. These symbols will affect the character with the variant symbol and the character that cannot be converted by the system, but we generally do not need to perform conversion control to this extent, the direct value is 0.

Lpwidecharstr: point to the Unicode string to be converted.

Cchwidechar: specifies the number of characters in the buffer zone directed by the lpwidecharstr parameter. If the value is-1, the string is set to a string with null as the end character and the length is automatically calculated.

Lpmultibytestr: indicates the buffer zone that receives the converted string.

Cchmultibyte: specify the maximum value of the buffer directed by the lpmultibytestr parameter (measured in bytes ). If the value is zero, the function returns the number of bytes required for the target buffer to which lpmultibytestr points. In this case, the lpmultibytestr parameter is usually null.

 

Lpdefaultchar and pfuseddefachar CHAR: The widechartomultibyte function uses these two parameters only when the widechartomultibyte function encounters a wide byte character that is not represented in the code page marked by the ucodepage parameter. If the wide byte character cannot be converted, this function uses the character pointed to by the lpdefaultchar parameter. If this parameter is null (this is the parameter value in most cases), the function uses the default character of the system. This default character is usually a question mark. This is dangerous for file names, because question marks are wildcards. The pfuseddefaultchar parameter points to a Boolean variable. if at least one character in a unicode string cannot be converted to an equivalent multi-byte character, the function sets this variable to true. If all characters are successfully converted, this function sets this variable to false. When the function returns to check whether the wide byte string is successfully converted, you can test the variable. These two parameters are basically set to null.

 

Return Value:If the function runs successfully and cchmultibyte is not zero, the returned value is the number of bytes written in the buffer directed by lpmultibytestr. If the function runs successfully and cchmultibyte is zero, the returned value is the number of bytes required to receive the buffer of the string to be converted.. If the function fails to run, the return value is zero. To obtain more error information, call the getlasterror function. It returns the following error codes:

Error_insufficient_bjffer; error_invalid_flags;

Error_invalid_parameter; error_no_unicode_translation.

 

// Convert a multi-byte string to a uniode string

Int multibytetowidechar (

Uint codePage,

DWORD dwflags,

Lpcstr lpmultibytestr,

Int cbmultibyte,

Lpwstr lpwidecharstr,

Int cchwidechar

);

The parameter is consistent with widechartomultibyte, that is, the last two parameters are missing.

 

Example:

Lpwstrlphello = l "Hello World ";

/* Obtain the required buffer size */

Uint nbuffersize;

Nbuffersize = widechartomultibyte (cp_acp, 0, lphello,-1, null, 0, null, null );

/* Apply for the required buffer */

Lpstrlptarget;

Lptarget = (lpstr) heapalloc (getprocessheap (), 0, sizeof (char) * nbuffersize );

/* Official conversion */

Widechartomultibyte (cp_acp, 0, lphello,-1, lptarget, nbuffersize, null, null );

// Do something -_-

/* Release the buffer */

Heapfree (getprocessheap (), 0, lptarget );

 

// It looks much better than writing like this

Lpwstrlphello = l "Hello World ";

Charszbuf [max_path];

Memset (szbuf, 0, sizeof (szbuf ));

Widechartomultibyte (cp_acp, 0, lphello, (INT) wcslen (lphello), szbuf, sizeof (szbuf), null, null );

Cout <szbuf <Endl;

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.