C + + GB2312 and UTF8 in Win32

Source: Internet
Author: User

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

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.