Use multibytetowidechar and widechartomultibyte to convert between short and wide characters

Source: Internet
Author: User

1. Usage Details

At the beginning of this article, we will briefly describe what short and wide characters are.
The so-called short character is represented by 8 bits. The typical application is ASCII code. the wide character, as its name implies, is a character represented by 16 bits, typically Unicode. for more information about ASCII and Unicode in windows, refer to these two classic books: Windows Program Design, Windows core programming. These two books provide a detailed explanation of these two types of characters.

It is difficult to convert wide characters into multiple short characters. However, we only need to master the essentials, as we can.
Okay, let's get started.

This is the multi-byte string we need to convert:
Char stext [20] = {"Multi-byte string! OK! "};

We need to know how many array spaces are required for converted wide characters. although in this mileage, we can directly define an array of 20*2 Characters in width, and in fact it will run very easily and happily. however, if there are more multi-byte strings and tens of thousands or even tens of thousands of strings, we will find that more and more memory will be wasted. therefore, it is definitely not a good idea to use the double number of multi-byte characters as the declaration of the bottom mark of the wide character array.
Fortunately, we can determine the array space we need.
We only need to set the fourth parameter of multibytetowidechar () to-1 to return the number of spaces in the required short character array:
DWORD dwnum = multibytetowidechar (cp_acp, 0, stext,-1, null, 0 );

Next, we only need to allocate the response array space:
Wchar_t * pwtext;
Pwtext = new wchar_t [dwnum];
If (! Pwtext)
{
Delete [] pwtext;
}

Next, we can start to convert it. Here we use the conversion to ASCII code as an example:
Multibytetowidechar (cp_acp, 0, pstext,-1, stext, dwsize );

Finally, remember to release the occupied memory after use:
Delete [] pstext;


Similarly, the width character is converted into multi-byte characters Code As follows:
Wchar_t wtext [20] = {L "wide character conversion instance! OK! "};
DWORD dwnum = widechartomultibyte (cp_oemcp, null, lpcwszstr,-1, null, 0, null, false );
Char * pstext;
Pstext = new char [dwnum];
If (! Pstext)
{
Delete [] pstext;
}
Widechartomultibyte (cp_oemcp, null, lpcwszstr,-1, pstext, dwnum, null, false );
Delete [] pstext;

If we have already allocated space, and the string is short, we can ignore the space that will be wasted. We just want to simply convert the short and wide characters to each other. Is there any easy way?
Win32 API does not have a function that meets this requirement, but we can encapsulate it ourselves:

// Configure //-------------------------------------------------------------------------------------
// Description:
// This function maps a character string to a wide-character (UNICODE) String
//
// Parameters:
// Lpcszstr: [in] pointer to the character string to be converted
// Lpwszstr: [out] pointer to a buffer that contains es the translated string.
// Dwsize: [in] size of the buffer
//
// Return values:
// True: Succeed
// False: Failed
//
// Example:
// Mbytetowchar (sza, SZW, sizeof (SZW)/sizeof (SZW [0]);
// Configure //---------------------------------------------------------------------------------------
Bool mbytetowchar (lpcstr lpcszstr, lpwstr lpwszstr, DWORD dwsize)
{
// Get the required size of the buffer that matches es the Unicode
// String.
DWORD dwminsize;
Dwminsize = multibytetowidechar (cp_acp, 0, lpcszstr,-1, null, 0 );

If (dwsize <dwminsize)
{
Return false;
}


// Convert headers from ASCII to Unicode.
Multibytetowidechar (cp_acp, 0, lpcszstr,-1, lpwszstr, dwminsize );
Return true;
}

// Configure //-------------------------------------------------------------------------------------
// Description:
// This function maps a wide-character string to a new character string
//
// Parameters:
// Lpcwszstr: [in] pointer to the character string to be converted
// Lpszstr: [out] pointer to a buffer that contains es the translated string.
// Dwsize: [in] size of the buffer
//
// Return values:
// True: Succeed
// False: Failed
//
// Example:
// Mbytetowchar (SZW, sza, sizeof (sza)/sizeof (sza [0]);
// Configure //---------------------------------------------------------------------------------------
Bool wchartombyte (maid, lpstr, DWORD dwsize)
{
DWORD dwminsize;
Dwminsize = widechartomultibyte (cp_oemcp, null, lpcwszstr,-1, null, 0, null, false );
If (dwsize <dwminsize)
{
Return false;
}
Widechartomultibyte (cp_oemcp, null, lpcwszstr,-1, lpszstr, dwsize, null, false );
Return true;
}


The usage is also very simple. The example is as follows:
Wchar_t wtext [10] = {L "function example "};
Char stext [20] = {0 };
Wchartombyte (wtext, stext, sizeof (stext)/sizeof (stext [0]);
Mbytetowchar (stext, wtext, sizeof (wtext)/sizeof (wtext [0]);

The disadvantage of these two functions is that they cannot dynamically allocate memory and may waste a lot of memory space when converting long strings. The advantage is that, it is very convenient to convert short strings without considering a waste of space.


2. The multibytetowidechar () function is garbled.

Some friends may have discovered that this function does not work properly in the standard wince4.2 or wince5.0 SDK simulator, and all the characters After conversion are garbled. modify the multibytetowidechar () parameter in time.
However, this is not a code issue. It is due to the customized operating system. this also happens if the default language of the customized operating system is not Chinese. the default language of the standard SDK is English. to solve this problem, you cannot simply change the "default language" of "region options" of the control panel. Instead, you must select "Chinese" as the default language during system customization ".
The default language is used for system customization:
Platform-> setting...-> locale-> default language, select "Chinese", and then compile.

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.