I use C + + writer under Windows to receive UTF8 string will occur in Chinese can not normally display on the console, so special to solve the UTF8 string in VC + + on the normal display.
1, the problem recurs, UTF-8 encoded under the string "Chinese" under Windows Display as, where the behavior UTF8 string, under the behavior GBK string:
2, Method: The UTF-8 format of the code into GBK encoding (need to Windows.h header file)
1 stringUTF8TOGBK (string&StrUtf8)2 {3 stringSTROUTGBK ="";4 intLen = MultiByteToWideChar (Cp_utf8,0, Strutf8.c_str (),-1Null0);5WCHAR *WSZGBK =NewWchar[len +1];6memset (WSZGBK,0, Len *2+2);7MultiByteToWideChar (Cp_utf8,0, Strutf8.c_str (),-1, WSZGBK, Len);8Len = WideCharToMultiByte (CP_ACP,0, WSZGBK,-1Null0, NULL, NULL);9 Char*PSZGBK =New Char[Len +1];Tenmemset (PSZGBK,0, Len +1); OneWideCharToMultiByte (CP_ACP,0, WSZGBK,-1, PSZGBK, len, null, NULL); ASTROUTGBK =PSZGBK; - Delete[]PSZGBK; - Delete[]WSZGBK; the returnSTROUTGBK; -}
The invocation example is as follows:
1 string STRGBK = UTF8TOGBK (strUTF8); // strUTF8 to UTF8 string 2 " string (UTF-8): "<< strUTF8 << Endl; 3 " string (GBK):%s\n ", Strgbk.c_str ());
The results are as follows:
Enclose the code in GBK format into the UTF-8 encoding function:
1 stringGBKToUTF8 (ConstSTD::string&STRGBK)2 {3 stringStrOutUTF8 ="";4WCHAR *str1;5 intn = MultiByteToWideChar (CP_ACP,0, Strgbk.c_str (),-1Null0);6STR1 =NewWchar[n];7MultiByteToWideChar (CP_ACP,0, Strgbk.c_str (),-1, STR1, n);8n = WideCharToMultiByte (Cp_utf8,0, STR1,-1Null0, NULL, NULL);9 Char* STR2 =New Char[n];TenWideCharToMultiByte (Cp_utf8,0, STR1,-1, STR2, n, null, NULL); OneStrOutUTF8 =str2; A Delete[]str1; -STR1 =NULL; - Delete[]str2; theSTR2 =NULL; - returnstrOutUTF8; -}
Windows VC + + display UTF-8 encoded Chinese