Conversion between wide bytes and narrow bytes

Source: Internet
Author: User

Conversion from wide to single-byte:Size_t Wcstombs ( Char *Mbstr, Const Wchar_t *Wcstr, Size_t Count );

Single-byte to wide byte:Size_t Mbstowcs ( Wchar_t *Wcstr, Const Char *Mbstr, Size_t Count );

The above two are standard C ++, and the following two seem to be Microsoft functions. However, the two functions can only be converted one by one, that is, only one character can be converted at a time. The following two methods are the conversion between a wide string and a single-byte string.

Wide-byte to multi-byte: WideCharToMultiByte // In fact, the multi-byte here is what we call char

Multi-byte to wide byte: MultiByteToWideChar // multi-byte, that is, ASCII single byte

These four methods.

The following example shows how to convert a wide string to a char string.

BSTR devname; // olewchar is actually wchar

Char * name = NULL;

PNCP-> get_Name (& devname );

DWORD n = WideCharToMultiByte (CP_OEMCP, NULL, devname,-1, NULL, 0, NULL, FALSE );

Name = new char [n];

WideCharToMultiByte (CP_OEMCP, NULL, devname,-1, name, n, NULL, FALSE );

Cout <name <endl;

Delete [] name;

The following describes how to convert a char string to a unicode string, that is, a wide character.

Char sText [20] = {"actually I am a so-called multi-byte string "};

DWORD dwNum = MultiByteToWideChar (CP_ACP, 0, sText,-1, NULL, 0 );

Wchar_t * pwText;

PwText = new wchar_t [dwNum];

MultiByteToWideChar (CP_ACP, 0, pwText,-1, sText, dwSize );

If (! PwText ){

Delete [] pwText;

}

 

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.