Conversion of cstring and char * in the Unicode character set (solving Chinese garbled characters)

Source: Internet
Author: User

There are many articles about the conversion of cstring to char * methods in UNICODE, but most of them are being reproduced from each other. After reading so many materials, the problem of garbled characters is still not solved, later, I found the correct method from a reply to a forum and would like to share it with you.

Summarize the Three conversion methods found on the Internet:

Method 1: Use the setlocale Function

Setlocale (lc_all, "CHS ");

Header file # include <locale. h>

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

Method 2: Use functions: T2A and w2a

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

// Declare the identifier uses_conversion;

// Call the function. Both T2A and w2a 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 identifiers, T2A and w2a.

Method 3: Use API: widechartomultibyte for conversion

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

// Note: the values of the following N and Len 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 the wide byte character. The size is calculated in bytes. intlen = widechartomultibyte (cp_acp, 0, STR, str. getlength (), null, 0, null, null );

// Apply for space for multi-byte character arrays. The array size is byte-based wide byte size char * pfilename = newchar [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'; // multi-byte 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!

 

----------------------------------------------------------------------------------

Bytes -----------------------------------------------------------------------------------

Bytes ------------------------------------------------------------------------------------

In VisualC ++. in net2005, the default character set format is Unicode. In vc6.0 and other projects, the default character set format is multi-byte character set (MBCS: Multi-bytecharacterset ), as a result, various types of character operations and functions that are very simple and practical in vc6.0 may report various errors when running in the vs2005 environment. in the net2005 environment, the Unicode Character Set cstring and char * are converted to each other, that is, the conversion between the Unicode Character Set and the MBCS character set.

1. Convert cstring to char * in Unicode *

Method 1: Use API: widechartomultibyte for conversion

Cstringstr = _ 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 INTN = Str. getlength (); // n = 14, Len = 18

// Obtain the size of the wide byte character. The size is 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 byte-based wide byte size char * pfilename = newchar [Len + 1]; // in bytes

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

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

Method 2: Use functions: T2A and w2a

Cstring STR = _ T ("d :\\ internal project \ qq.bmp ");

// Declare the identifier uses_conversion;

// Call the function. Both T2A and w2a 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>

2. Convert char * in Unicode to cstring

Method 1: Use API: multibytetowidechar for conversion

Char * pfilename = "d :\\ internal project \ qq.bmp ";

// Calculate the size of the char * array, in bytes. One Chinese Character occupies two bytes: int charlen = strlen (pfilename );

// Calculate the size of Multi-byte characters, measured in characters. Int Len = multibytetowidechar (cp_acp, 0, pfilename, charlen, null, 0 );

// The request space is a wide byte character array. The array size is the size of multiple byte characters calculated in bytes. tchar * Buf = new tchar [Len + 1];

// Convert multi-byte encoding to multibyteteowidechar (cp_acp, 0, pfilename, charlen, Buf, Len );

Buf [Len] = '\ 0'; // Add the end of the string. Note that it is not Len + 1 // convert the tchar array to cstring pwidechar; pwidechar. append (BUF );

// Delete the buffer [] Buf;

Method 2: Use functions a2t and a2w

Char * pfilename = "d :\\ internal project \ qq.bmp ";

Uses_conversion; cstring S = a2t (pfilename );

// Cstring S = a2w (pfilename );

Method 3: Use a _ t macro to convert a string to a wide character

// Multi-byte character set, which can be compiled in vc6 and vc7, but cannot be passed in vs2005. The default value is Unicode Character Set // afxmessagebox ("failed to load data", 0 );

// Use text ("") or _ T ("") to write code. afxmessagebox (_ T ("failed to load data") is common in Unicode and non-Unicode programs "), 0 );

Note: Direct conversion is acceptable in MBCS-based projects, but direct conversion is not feasible in projects based on UNICODE character sets. cstring stores data in unicode format, only the first character is returned for forced type conversion.

Trackback: http://topic.csdn.net/t/20050608/14/4068106.html

Http://houjixin.blog.163.com/blog/static/35628410200922595225193/

Http://hi.baidu.com/flobert_young/blog/item/6f93fd0a3ec83f1894ca6b50.html

Http://hi.baidu.com/proworkspace/blog/item/50cdee44b03f1d86b2b7dc44.html

Http://msdn.microsoft.com/en-us/library/ms235631.aspx

Original article address:

Http://blog.csdn.net/linrulei11/article/details/7824954

Conversion of cstring and char * in the Unicode character set (to solve Chinese garbled characters)

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.