Let's take a look at this article on Windows encoding: http://blog.csdn.net/shyboy_nwpu/article/details/4431668
Take a look at this description of two function parameters and usage: http://www.cnblogs.com/wind-net/archive/2012/10/10/2718340.html
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 used.
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.
1. ANSI to Unicode
1 wstring ansitounicode (const string& str) 2 {3 int len = 0; 4 len = Str.length (); 5 int unicodelen =:: Multibyteto Widechar (CP_ACP, 6 0, 7 str.c_str (), 8 -1, 9 null,10 0); wchar_t * punicode; punicode = new WC HAR_T[UNICODELEN+1]; Memset (punicode,0, (unicodelen+1) *sizeof (wchar_t)); : MultiByteToWideChar (cp_acp,15 0,16 str.c_str (), -1,18 (LPWSTR) punicode,19 Unicodelen ); wstring RT; RT = (wchar_t*) punicode;22 Delete punicode; The return RT; 24}
2. Unicode to ANSI
1 string unicodetoansi (const wstring& str) 2 {3 char* pelementtext; 4 int itextlen; 5//Wide char to Multi Char 6 Itextlen = WideCharToMultiByte (CP_ACP, 7 0, 8 str.c_str (), 9 -1,10 null,11 0,12 NULL, ( NULL); pelementtext = new Char[itextlen + 1];15 memset ((void*) pelementtext, 0, sizeof (char) * (Itextlen + 1);: WideCharToMultiByte (cp_acp,17 0,18 str.c_str (), -1,20 pelementtext,21 itextlen,22 null,23 NULL); string strtext;25 StrText = pelementtext;26 delete[] pelementtext;27 return STRTEXT;28}
3. UTF-8 to Unicode
1 wstring utf8tounicode (const string& str) 2 {3 int len = 0; 4 len = Str.length (); 5 int unicodelen =:: Multibyteto Widechar (Cp_utf8, 6 0, 7 str.c_str (), 8 -1, 9 null,10 0); wchar_t * punicode; punicode = new W CHAR_T[UNICODELEN+1]; Memset (punicode,0, (unicodelen+1) *sizeof (wchar_t)); : MultiByteToWideChar (cp_utf8,15 0,16 str.c_str (), -1,18 (LPWSTR) punicode,19 Unicodelen); wstring RT; RT = (wchar_t*) punicode;22 Delete punicode; The return RT; 24}
4. Unicode to UTF-8
1 string UnicodeToUTF8 (const wstring& str) 2 {3 char* pelementtext; 4 int itextlen; 5//Wide char to Multi Char 6 Itextlen = WideCharToMultiByte (Cp_utf8, 7 0, 8 str.c_str (), 9 -1,10 null,11 0,12 NULL, ( NULL); pelementtext = new Char[itextlen + 1];15 memset ((void*) pelementtext, 0, sizeof (char) * (Itextlen + 1);: WideCharToMultiByte (cp_utf8,17 0,18 str.c_str (), -1,20 pelementtext,21 itextlen,22 null,23 NULL); string strtext;25 StrText = pelementtext;26 delete[] pelementtext;27 return Strtext;28 29}
Example: http://download.csdn.net/detail/qq_23992597/9696223 http://blog.csdn.net/qq_23992597/article/details/53385756
Use of WideCharToMultiByte and MultiByteToWideChar functions