Windows VC + + display UTF-8 encoded Chinese

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.