1 stringANSItoUTF8 (Const Char*Stransi)2 {3 //gets the buffer size required after converting to a wide byte, creating a wide-byte buffer, 936 for the Simplified Chinese GB2312 code page4 intNlen = MultiByteToWideChar (CP_ACP, NULL, Stransi,-1, NULL, NULL);5WCHAR *wszbuffer =NewWchar[nlen +1];6Nlen = MultiByteToWideChar (CP_ACP, NULL, Stransi,-1, Wszbuffer, nlen);7Wszbuffer[nlen] =0;8 //gets the size of the buffer needed to convert to UTF8, creating a multi-byte buffer9Nlen = WideCharToMultiByte (Cp_utf8, NULL, Wszbuffer,-1, NULL, NULL , NULL, or NULL);TenCHAR *szbuffer =NewChar[nlen +1]; OneNlen = WideCharToMultiByte (Cp_utf8, NULL, Wszbuffer,-1, szbuffer, Nlen, NULL, and NULL); ASzbuffer[nlen] =0; - - stringS1 =Szbuffer; the //Memory Cleanup - Delete[]wszbuffer; - Delete[]szbuffer; - returnS1; + } - + stringUtf8toansi (Const Char*strUTF8) A { at //gets the buffer size required to convert to a multi-word section, creating a multi-byte buffer - intNlen = MultiByteToWideChar (Cp_utf8, NULL, StrUTF8,-1, NULL, NULL); -WCHAR *wszbuffer =NewWchar[nlen +1]; -Nlen = MultiByteToWideChar (Cp_utf8, NULL, StrUTF8,-1, Wszbuffer, nlen); -Wszbuffer[nlen] =0; - inNlen = WideCharToMultiByte (936, NULL, Wszbuffer,-1, NULL, NULL , NULL, or NULL); -CHAR *szbuffer =NewChar[nlen +1]; toNlen = WideCharToMultiByte (936, NULL, Wszbuffer,-1, szbuffer, Nlen, NULL, and NULL); +Szbuffer[nlen] =0; - the stringS1 =Szbuffer; * //Clean up memory $ Delete[]szbuffer;Panax Notoginseng Delete[]wszbuffer; - returnS1; the}
Reproduced
C + + GB2312 and UTF8 in Win32