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

Source: Internet
Author: User

Conversion of cstring to char * in UnicodeOfArticleThere 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, w2aDetails

Click me

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.
             Intlen = 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 = 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 ';  // 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. WidechartomultibyteClick me

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 the API:WidechartomultibyteConvert

             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 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 = 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, 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>

2. Convert char * in Unicode to cstring

Method 1: Use the API:MultibytetowidecharConvert

              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 );

             // Request space for the 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, pfilename, charlen, Buf, Len);

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

             // Delete the buffer
             Delete [] Buf;

Method 2: Use functions:A2t, a2w

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

             Uses_conversion;
             Cstring S = a2t (pfilename );

             // Cstring S = a2w (pfilename );

Method 3: Use_ T macroTo convert a string to a wide character.

           // Multi-byte character set. statements that 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 );

            // WriteCodeUse text ("") or _ T (""), text in Unicode and non-UnicodeProgramUniversal
            Afxmessagebox (_ T ("failed to load data"), 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

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.