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