Convert a multi-byte string to Unicode on Windows

Source: Internet
Author: User

We use the Windows function MultiByteToWideChar to convert multibyte strings to wide strings. As shown below:

int MultiByteToWideChar (    UINT      ucodepage,       //identifies a code page value associated with a multibyte character    DWORD     dwFlags,         // Additional control is allowed, but generally passed    in 0 pcstr     pmultibytestr,//    string to convert int       cbmultibyte,     / The length of the string, if passed-1, automatically determines the length    pwstr     pwidecharstr,    //The resulting Unicode string is passed into the specified memory buffer     int       Cchwidechar);    Specifies the maximum length of the buffer

Specific steps to convert:

1. Call MultiByteToWideChar,

Pass NULL to the PWIDECHARSTR parameter,

Pass in 0 for the Cchwidechar parameter,

The Cbmultibyte parameter is passed in-1.

2. Allocate a block of memory sufficient to accommodate the converted Unicode string.

Its size is the return value of the previous MultiByteToWideChar call multiplied by sizeof (wchar_t).

3. Call MultiByteToWideChar again, this time

The buffer address is passed in as the value of the PWIDECHARSTR parameter.

The return value of the first MultiByteToWideChar call is multiplied by sizeof (wchar_t) to get the size passed in as the value of the Cchwidechar parameter.

4. Use the converted string.

5. Releases the block of memory occupied by the Unicode string.

Convert a multi-byte string to Unicode on Windows

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.