Wide-byte and single-byte conversion cstring and char * Conversion in Unicode Character Set

Source: Internet
Author: User

In Visual C ++. in net2005, the default character set format is Unicode, but in vc6.0 and other projects, the default character set format is multi-byte character set (MBCS: Multi-byte character set ), 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. Here we summarize the errors in Visual C ++. 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

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

Pfilename [Len + 1] = '/0'; // multi-byte 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. 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 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 );

// 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 it is not Len + 1.

// Convert the tchar array to cstring
Cstring pwidechar;
Pwidechar. append (BUF );

// Delete the buffer
Delete [] 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. 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.

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.