Unicode char * To cstring, using multibytetowidechar for conversion, Chinese garbled Solution

Source: Internet
Author: User

Using macro functions such as a2w and a2t is a common method, but Chinese characters are garbled, so multibytetowidechar is used for conversion.

 

 //  Calculates the size of the char * array, in bytes. One Chinese Character occupies two bytes.      Int Charlen = Strlen (stext );  //  Calculate the size of Multi-byte characters by character.      Int Len = multibytetowidechar (cp_acp, 0 , Stext, charlen, null, 0  );  //  Applies for space for a wide byte character array. The array size is the size of Multi-byte characters calculated in bytes. Tchar * Buf =New Tchar [Len + 1  ];  //  Convert multi-byte encoding to wide-byte encoding Multibytetowidechar (cp_acp, 0  , Stext, charlen, Buf, Len); Buf [Len] = '  \ 0  ' ; //  Add the end of the string. Note that it is not Len + 1.  //  Convert a tchar array to a cstring Cstring pwidechar; pwidechar. append (BUF );  //  Delete a buffer Delete [] Buf;

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.