Windows API tour: multibytetowidechar function and widechartomultibyte Function

Source: Internet
Author: User

We generally use the Windows function multibytetowidechar to convert a multi-byte string to a wide character string. The function prototype is as follows:

Int multibytetowidechar (<br/>__ in uint codePage, // a code page value associated with a multi-byte string, usually set to cp_acp <br/>__ in DWORD dwflags, // specify the extra conversion control, which is generally 0 <br/>__ in lpcstr lpmultibytestr, // multi-byte string to be converted <br/>__ in int cbmultibyte, // The number of multi-byte string bytes. If it is-1, the string length is automatically determined <br/>__ out lpwstr lpwidecharstr, // buffer of the Unicode string after conversion <br/>__ in int cchwidechar // Maximum length of the receiving buffer (number of characters) <br/> );

Follow these steps to convert a multi-byte string to the unicode format:

1) Call multibytetowidechar to input NULL for lpwidecharstr, 0 for cchwidechar, and-1 for cbmultibyte;

2) allocate a memory that is sufficient to accommodate the converted Unicode string. Its size is 1) Multiply the return value of the function by sizeof (wchar_t );

3) Call multibytetowidechar again. This time, pass in the buffer address as the value of the lpwidecharstr parameter. 1) Multiply the return value of the function by sizeof (w_char_t) and the obtained size as the value of the cchwidechar parameter;

4) use the converted string;

5) release memory blocks occupied by Unicode strings.

 

The following is a function for converting a multi-byte string to a unicode string:

/*************************************** * ************** <Br/> * function: convert multibyte character set to wide-character set <br/> * Param: pwstr -- [out] points to a buffer that contains es the translated buffers. <br/> * pstr -- [in] points to the multibyte character set (or string) to be converted. <br/> * Len -- [in] specify the size in bytes of the string pointed to by the pstr <br/> * parameter, or it can be-1 if the string is NULL terminated. <br/> * isend -- [in] specify whether you add'/0' to the end of converted array or not. <br/> * return: the length of converted set (or string) <br/> ************************************ * *****************/<br/> int towidestring (wchar * & pwstr, const char * pstr, int Len, bool isend) <br/>{< br/> assert_pointer (pstr, char ); <br/> assert (LEN> = 0 | Len =-1); <br/> int nwidelen = multibytetowidechar (cp_acp, 0, pstr, Len, null, 0); <br/> If (LEN =-1) <br/> {<br/> -- nwidelen; <br/>}< br/> If (nwidelen = 0) <br/>{< br/> return 0; <br/>}< br/> If (isend) <br/>{< br/> pwstr = new wchar [(nwidelen + 1) * sizeof (wchar)]; <br/> zeromemory (pwstr, (nwidelen + 1) * sizeof (wchar )); <br/>}< br/> else <br/> {<br/> pwstr = new wchar [nwidelen * sizeof (wchar)]; <br/> zeromemory (pwstr, nwidelen * sizeof (wchar); <br/>}< br/> multibytetowidechar (cp_acp, 0, pstr, Len, pwstr, nwidelen); <br/> return nwidelen; <br/>}

Correspondingly, we use the widechartomultibyte function to convert a wide character string to a multi-byte string. The function prototype is as follows:

Int widechartomultibyte (<br/> _ in uint codePage, // identifies the code page associated with the newly converted string <br/> _ in DWORD dwflags, // specify the extra conversion control, which is generally set to 0 <br/> _ in lpcwstr lpwidecharstr, // memory address of the string to be converted <br/> _ in int cchwidechar, // The length of the above string (number of characters ), if-1 is passed, a function is provided to determine the length of the string <br/> _ out lpstr lpmultibytestr, // obtain the multi-byte string buffer after conversion <br/> _ in int cbmultibyte, // The maximum value (number of bytes) of the buffer above. If 0 is passed, the function returns the size required for the target buffer. The returned value is the number of bytes required after the conversion is successful. You do not need to multiply the value by sizeof... <Br/> _ in lpcstr lpdefaultchar, // when a wide character cannot be converted, the function uses the character specified by this parameter. If it is null, the function uses the default character <br/> _ out lpbool lpuseddefaultchar // In the wide character string, if at least one character cannot be converted to the corresponding multi-byte format, the function sets this value to true. We usually pass in null <br/>); <br/>

The following is a function we encapsulate to convert a multi-byte string to a wide character string:

/*************************************** * ************** <Br/> * function: convert wide-character set to multibyte character set <br/> * Param: pstr -- [in] points to a buffer that contains es the translated buffer. <br/> * pwstr -- [out] points to the wide character set (or string) to be converted. <br/> * Len -- [in] specify the size in bytes of the string pointed to by the pwstr <br/> * parameter, or it can be-1 if the string is NULL terminated. <br/> * isend -- [in] specify whether you add'/0' to the end of converted array or not. <br/> * return: the length of converted set (or string) <br/> ************************************ * *****************/<br/> int tomultibytes (char * & pstr, const wchar * pwstr, int Len, bool isend) <br/>{< br/> assert_pointer (pwstr, wchar ); <br/> assert (LEN> = 0 | Len =-1); <br/> int nchars = widechartomultibyte (cp_acp, 0, pwstr, Len, null, 0, null, null); <br/> If (LEN =-1) <br/>{< br/> -- nchars; <br/>}< br/> If (nchars = 0) <br/>{< br/> return 0; <br/>}< br/> If (isend) <br/>{< br/> pstr = new char [nchars + 1]; <br/> zeromemory (pstr, nchars + 1); <br/>}< br/> else <br/>{< br/> pstr = new char [nchars]; <br/> zeromemory (pstr, nchars); <br/>}< br/> widechartomultibyte (cp_acp, 0, pwstr, Len, pstr, nchars, null, null); <br/> return nchars; <br/>}

The preceding two encapsulated functions are used as follows:

1)

 

 

 

Char * pstr = "asce1885"; <br/> wchar * pwstr; <br/> int nwidelen = towidestring (pwstr, pstr,-1, true );

2)

Wchar * pwstr = _ T ("asce1885"); <br/> char * pstr; <br/> int nwidelen = tomultibytes (pstr, pwstr,-1, true );

References:

Windows via C/C ++

Http://www.codeproject.com/Tips/128870/Useful-function-for-conversion-between-MBCS-and-WC.aspx

 

Related Article

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.