Use of WideCharToMultiByte and MultiByteToWideChar functions

Source: Internet
Author: User

In order to support Unicode encoding, it is necessary to convert between multibyte and wide bytes. These two system functions need to specify a code page when they are in use, encounter garbled problems during the actual application, and then re-read Windows core programming to summarize the correct usage. The WideCharToMultiByte code page is used to mark the code page associated with the newly converted string. The MultiByteToWideChar code page is used to mark a code page related to a multibyte string. Common code pages are made up of CP_ACP and Cp_utf8 two. The conversion between ANSI and Unicode is achieved using the CP_ACP code page. The conversion between UTF-8 and Unicode is achieved using the Cp_utf8 code page. Here is the code implementation: 1. ANSI to UnicodeWstring ansitounicode (const string& str) { int  len = 0; len = Str.length ();  int  unicodele n =:: MultiByteToWideChar (cp_acp,            0,            St R.c_str (),            -1,            null,            0);   wchar_t *  pUnicode;   punicode = new  wchar_t[unicodeLen+1];   memset (punicode,0, (unicodelen+1) *sizeof (wchar_t));   ::multibytetowidechar (cp_acp,         0,         STR.C_ STR (),         -1,          (LPWSTR) punicode,         unicodelen);   wstring  rt;   rt = (wchar_t*) punicode; delete  pUnicode;   return  rt;  } 2.  unicode to ANSI String Unicodetoansi (const wstring& str) { char*     pelementtext; int    itextlen;& nbsp;//wide char to multi Char itextlen = WideCharToMultiByte (cp_acp,         0,  &NB Sp      STR.C_STR (),         -1,         NULL,         0,NULL,         null)  pelementtext = new Char[itextlen + 1]; mem Set ((void*) pelementtext, 0, sizeof (char) * (Itextlen + 1));  ::widechartomultibyte (cp_acp,    &NBSP ;    0,         STR.C_STR (),         -1,      &NB Sp  pElementText,         iTextLen,         NULL,         null);  string strtext; strtext = pelementtext; delete[] Pelementtext; return StrText;} 3.  utf-8 to Unicode Wstring utf8tounicode (const string& str) { int  len = 0; len = Str.length ();  int  unicodele n =:: MultiByteToWideChar (cp_utf8,            0,            s Tr.c_str (),            -1,            null,    &NBSP ;       0);   wchar_t *  pUnicode;   punicode = new  wchar_t[unicodeLen+1];   memset (punicode,0, (unicodelen+1) *sizeof (wchar_t));   ::multibytetowidechar (cp_utf8,         0,         STR.C_ STR (),         -1,          (LPWSTR) punicode,         unicodelen);   wstring  rt;   rt = (wchar_t*) punicode; delete  pUnicode;   return  rt;  } 4.  unicode to UTF-8     String UnicodeToUTF8 (const wstring& str) { char*     pelementtext; int    itextlen;& nbsp;//wide char to multi Char itextlen = WideCharToMultiByte (cp_utf8,         0,  &N Bsp      STR.C_STR (),         -1,         NULL,         0,         NULL,         null);  pelementtext = new Char[itextlen + 1]; memset ((void*) pelementtext, 0, sizeof (char) * (Itextlen + 1));  ::widechartomul Tibyte (cp_utf8,         0,         STR.C_STR (),      &NB Sp  -1,         pElementText,         iTextLen,      & nbsp  NULL,         null);  string strtext; strtext = pelementtext; delete[] pelementtext; return strText;}

From for notes (Wiz)

Use of WideCharToMultiByte and MultiByteToWideChar functions

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.