c++ - utf-8 to/from utf-16 problem -
I am based on an answer on these two conversion functions and stack overflow, but changing back and forth does not work :
std :: wstring MultiByteToWideString (const char * szSrc) {unsigned int iSizeOfStr = MultiByteToWideChar (CP_ACP, 0, szSrc, -1, NULL, 0); Wchar_t * wszTgt = new wchar_t [iSizeOfStr]; If (! WszTgt) thrust (0); Multibytovider (CP_ACCP, 0, SSRC, -1, WSJTTGT, ISIEFFST); Std :: wstring wstr (wszTgt); Delete (wszTgt); Return (wstr); } Std :: string WideStringToMultiByte (const wchar_t * wszSrc) {int iSizeOfStr = WideCharToMultiByte (CP_ACP, 0, wszSrc, -1, zero, 0, zero, zero); Four * szTgt = new four [iSizeOfStr]; If (! SzTgt) Returns (NULL); Wide-Chartamultibout (CP_ACP, 0, VSSSCR, -1, SGTGT, ISIEF, Null, Faucet); Std :: string str (szTgt); Delete (szTgt); Return (STR); } [...] // は て な ブ utf-16 wchar_t wTestUTF16 [] = L "\ u306f \ u3066 \ u306a \ u30d6 \ u306f \ u306f"; // shows the text correctly: messagebox (zero, wTestUTF16, L "message", MB_OK); // Convert UTF 8, and back to UTF-16 STD :: String Strutif 8 = WideStringTommeltbyte (WTTTF16); Std :: wstring wstrUTF16 = multibytowoldstring (strutF8.c_str ()); // It does not show proper text should be similar to the first message box: Message box (NULL, wstrUTF16.c_str (), L "Message", MB_OK);
Check for docs converts to CP_ACP using the current system code page. It's very lossy you want CP_UTF8
Comments
Post a Comment