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, stext,-1, pwtext, dwsize );
Finally, remember to release the occupied memory after use:
Delete [] pwtext;
Similarly, the Code for converting a wide character into a multi-byte character is 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;