Conversion of Unicode strings and multi-byte strings

Source: Internet
Author: User

I. Let's take a look at the Declaration of the two functions:

1. Multi-byte string and wide String Conversion
  int MultiByteToWideChar(
Uint codePage, // code page, usually set to cp_acp
DWORDDwflags,// Character-type options, usually set to 0
LpcstrLpmultibytestr,// String to map, pointing to a multi-byte string
IntCbmultibyte,// Number of bytes in string, the length of a Multi-byte string (the number of bytes. It can be set to-1 when it ends with 0)
Lpwstr lpwidecharstr, // wide-character buffer, which stores the converted wide string buffer.
IntCchwidechar// Size of buffer, maximum length of the wide string buffer (number of characters)
  );
 
1. Conversion between a wide string and a multi-byte string
  int WideCharToMultiByte(
Uint codePage, // code page, usually set to cp_acp
DWORDDwflags,// Performance and mapping flags, usually set to 0
LpcwstrLpwidecharstr,// Wide-character string, pointing to a wide string
IntCchwidechar,// Number of chars in string, the length of the wide string (number of characters)
LpstrLpmultibytestr,// Buffer for new string, storing the converted multi-byte String Buffer
IntCbmultibyte,// Size of buffer, maximum length of Multi-byte string buffer (bytes)
LpcstrLpdefaultchar,// Default for unmappable chars, the string displayed by the character that fails to be converted, which is generally set to null
LpboolLpuseddefaultchar// Set when default char used. If character conversion fails, the value is true, which is generally set to null.
  );

2. Check the usage:# Include <stdlib. h> # include <locale. h> # include <windows. h> int main (INT argc, char * argv []) {setlocale (lc_all ,""); // otherwise, the Chinese char ansistr [] = "this is an ANSI string"; printf ("ansistr: % s \ n", ansistr) cannot be output ); wchar_t widestr [] = l "this is a unicode string"; wprintf (L "widestr: % s \ n", widestr ); // convert the ANSI string to the Unicode string int Len = multibytetowidechar (cp_acp, 0, ansistr,-1, null, 0 );//
Obtain the required length of the converted Unicode string wchar_t * buf1 = (wchar_t *) calloc (Len, sizeof (wchar_t ));
// Allocate the buffer multibytetowidechar (cp_acp, 0, ansistr,-1, buf1, Len); // start to convert wprintf (L "converted Unicode string: % s \ n ", buf1); // output the converted string free (buf1); // release the buffer // convert the Unicode string to the ANSI string Len = widechartomultibyte (cp_acp, 0, widestr,-1, null, 0, null, null );//
Obtain the length of the converted ANSI string char * buf2 = (char *) calloc (Len, sizeof (char ));
// Allocate the buffer widechartomultibyte (cp_acp, 0, widestr,-1, buf2, Len, null, null );//
Start to convert printf ("converted ANSI string: % s \ n", buf2 );//
Output the converted string free (buf2 );//
Release buffer return 0 ;}

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.