Unicode cstring to char *, using widechartomultibyte for conversion, Chinese garbled Solution

Source: Internet
Author: User

 

Conversion of cstring to char * in UnicodeArticleThere are many, but most of them are being reproduced from each other. After reading so many materials, we still haven't solved the problem of Garbled text. Later, we found the correct method in a reply from a forum, I would like to share it with you.

Summarize the Three conversion methods found on the Internet:

Method 1: Use FunctionsSetlocale

Setlocale (lc_all, "CHS ");

Header file # include <locale. h>

The idea of this method is:Configure the region information. You can set it when you need to input and output Chinese characters. For more information, see setlocale.

 

Method 2: Use functions:T2A, w2a

Cstring STR = _ T ("D: // internal project // qq.bmp ");

// Declare the identifier
Uses_conversion;

// Call the function. T2A and w2a both support character conversion in ATL and MFC.
Char *
Pfilename = T2A (STR );
// Char * pfilename =
W2a (STR); // conversion is also possible

Note: Sometimes you may need to add reference # include <afxpriv. h>

When using this method, pay attention to declaring the identifier,T2A, w2aClick me for details

Method 3: Use the API:WidechartomultibyteConvert

Cstring STR = _ T ("D: // internal project // qq.bmp ");

// Note: The values of N and Len below are different in size. N is calculated by character, and Len is calculated by byte.
Int n =
Str. getlength (); // n = 14, Len = 18

// Obtain the size of a wide byte, measured in bytes.
Int Len
= Widechartomultibyte (cp_acp, 0, STR, str. getlength (), null, 0, null, null );

// Apply for space for multi-byte character arrays. The array size is the size of the wide byte calculated in bytes.
Char *
Pfilename = new char [Len + 1]; // in bytes

// Convert the wide-byte encoding to multi-byte encoding
Widechartomultibyte (cp_acp, 0, STR, str. getlength (), pfilename, Len, null, null );


Widechartomultibyte (cp_acp, 0, STR, str. getlength () + 1, pfilename, Len + 1
, Null, null );

Pfilename [Len + 1] = '/0 ';
// The multibyte character ends with '/0'

// Thanks for the reminder downstairs. Please modify it.

Pfilename [Len] = '\ 0 ';// The multibyte character ends with '\ 0'

These three methods are quite reliable, and many people have verified that they can be successful. However, when I use them, they are miserable, and none of them work. After careful consideration, the third method should be foolproof and the safest method. After careful searching, it turns out that the parameter is faulty. The yellow color is marked out by a wide range of methods circulating on the Internet, widechartomultibyte (cp_acp, 0, STR, str. getlength () + 1, pfilename, Len + 1, null, null); is the method of my successful verification. As to why, let everyone think about it. Widechartomultibyte

The younger brother is not easy to learn. The writing is incorrect. please correct me!

 

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.