Differences between wchar_t, Char, and wchar

Source: Internet
Author: User
Tags ole

1. Differences among wchar_t, Char, and wchar

 

ANSI: Char. Available string processing functions: strcat (), strcpy (), strlen (), and other functions with str headers.

UNICODE: wchar_t is the data type of Unicode characters. It is actually defined in:

Typedef unsigned short wchar_t;

In addition, the header file has the following definition: typedef wchar_t wchar; so wchar is actually wchar_t

Wchar_t can be used as a string processing function: wcscat (), wcscpy (), wcslen (), and other functions that are headers with WCS. To enable the compiler to recognize Unicode strings, you must add an "L" to the front, for example, wchar_t * sztest = l "this is a unicode string .";

 

Char c = 'a'; // C stores character a, which occupies one byte

Wchar_t WC = L 'a'; // WC stores the wide character a, which occupies two bytes. Note that 'A' represents character a, and l'a' represents character.

2. tchar

_ Unicode macros (with underscores) are provided in the C language, and Unicode macros (without underscores) are provided in windows. If _ Unicode macros and Unicode macros are specified, the system automatically switches to the Unicode version. Otherwise, the system compiles and runs in ANSI mode. Only macros are defined and cannot be automatically converted. It also requires support for a series of character definitions.

1. tchar

If a unicode macro is defined, tchar is defined as wchar_t.

Typedef wchar_t tchar;

Otherwise, tchar is defined as char typedef char tchar;

2. lptstr

If a unicode macro is defined, lptstr is defined as lpwstr.

Typedef lptstr lpwstr;

Otherwise, tchar is defined as char typedef lptstr lpstr;

Note: When using a String constant, use _ text ("mystr") or _ T ("") to support automatic conversion of the system.

 

3. BSTR

BSTR is a string with a length prefix, which is mainly managed by the operating system. it is mainly used to deal with VB (string in VB refers to it). There are many API functions to operate on it. for example, sysallocstring and sysfreestring.

The classes encapsulated in VC, such as _ bstr_t and ccombstr in ATL.

A bstr consists of a header and a string. the header contains the length of the string, which can contain embedded null values.

BSTR is transmitted as a pointer. (A pointer is a variable that contains the memory address of another variable, rather than data .) BSTR is Unicode, that is, each character requires two bytes. BSTR usually ends with a two-byte null character. Wstr is a wide character. In double byte format, a character BSTR is used to be compatible with the original basic character. Its first four bytes are of its length and end with '\ 0.

 

4. Further define the type of the string and Its pointer

Since the function list in the Win32 API document uses the common name of the function (for example, "setwindowtext"), all strings are defined using tchar. (Except for the Unicode-only API introduced in XP ). Some common typedefs are listed below. You can see them in msdn.

Type meaning in MBCS builds meaning in Unicode builds

Wchar wchar_t

Lpstr char *

Lpcstr const char *

Lpwstr wchar_t *

Lpcwstr wchar_t *

Tchar wchar_t

Lptstr tchar *

Lpctstr const tchar *

 

5. Mutual Conversion

(1) convert char * To cstring

If char * is converted to cstring, you can use cstring: format in addition to direct value assignment. For example:

Char charray [] = "this is a test ";

Char * P = "this is a test ";

Or

Lpstr P = "this is a test ";

Or in the use of Unicode applications that have been defined

Tchar * P = _ T ("this is a test ");

Or

Lptstr P = _ T ("this is a test ");

Cstring thestring = charray;

Thestring. Format (_ T ("% s"), charray );

Thestring = P;

 

(2) convert cstring to char *

If the cstring type is converted to the char * (lpstr) type, the following three methods are often used:

Method 1: use forced conversion. For example:

Cstring thestring ("this is a test ");

Lptstr lpsz = (lptstr) (lpctstr) thestring;

Method 2: Use strcpy. For example:

Cstring thestring ("this is a test ");

Lptstr lpsz = new tchar [thestring. getlength () + 1];

_ Tcscpy (lpsz, thestring );

It should be noted that the second parameter of strcpy (or _ tcscpy of Unicode/MBCS) is const wchar_t * (UNICODE) or const char * (ANSI ), the system compiler will automatically convert it.

Method 3: Use cstring: getbuffer. For example:

Cstring S (_ T ("this is a test "));

Lptstr P = S. getbuffer ();

// Add the code using P here

If (P! = NULL) * P = _ T ('\ 0 ');

S. releasebuffer ();

// Release immediately after use, so that other cstring member functions can be used.

 

(3) convert BSTR to char *

Method 1: Use convertbstrtostring. For example:

# Include

# Pragma comment (Lib, "comsupp. lib ")

Int _ tmain (INT argc, _ tchar * argv []) {

BSTR bstrtext =: sysallocstring (L "test ");

Char * lpsztext2 = _ com_util: convertbstrtostring (bstrtext );

Sysfreestring (bstrtext); // release after use

Delete [] lpsztext2;

Return 0;

}

Method 2: Use the _ bstr_t value assignment operator to overload. For example:

_ Bstr_t B = bstrtext;

Char * lpsztext2 = B;

 

(4) convert char * To BSTR

Method 1: Use API functions such as sysallocstring. For example:

BSTR bstrtext =: sysallocstring (L "test ");

BSTR bstrtext =: sysallocstringlen (L "test", 4 );

BSTR bstrtext =: sysallocstringbytelen ("test", 4 );

Method 2: Use colevariant or _ variant_t. For example:

// Colevariant strvar ("this is a test ");

_ Variant_t strvar ("this is a test ");

BSTR bstrtext = strvar. bstrval;

Method 3: Use _ bstr_t, which is the simplest method. For example:

BSTR bstrtext = _ bstr_t ("this is a test ");

Method 4: Use ccombstr. For example:

BSTR bstrtext = ccombstr ("this is a test ");

Or

Ccombstr BSTR ("this is a test ");

BSTR bstrtext = BSTR. m_str;

Method 5: Use convertstringtobstr. For example:

Char * lpsztext = "test ";

BSTR bstrtext = _ com_util: convertstringtobstr (lpsztext );

(5) convert cstring to BSTR

Generally, cstringt: allocsysstring is used. For example:

Cstring STR ("this is a test ");

BSTR bstrtext = Str. allocsysstring ();

...

Sysfreestring (bstrtext); // release after use

 

(6) convert BSTR to cstring

Generally, you can perform the following operations:

BSTR bstrtext =: sysallocstring (L "test ");

Cstringa STR;

Str. Empty ();

STR = bstrtext;

Or

Cstringa STR (bstrtext );

 

(7) Conversion between ANSI, Unicode, and wide characters

Method 1: Use multibytetowidechar to convert ANSI to Unicode, and use widechartomultibyte to convert Unicode to ANSI.

Method 2: Use "_ t" to convert ANSI to a "general" string and use "L" to convert ANSI to Unicode, in the hosted C ++ environment, you can use s to convert an ANSI string to a string * object. For example:

Tchar tstr [] = _ T ("this is a test ");

Wchar_t wszstr [] = l "this is a test ";

String * STR = s "this is a test ";

Method 3: Use the conversion macro and class of ATL 7.0. Based on the original 3.0, atl7.0 has improved and added many Character String Conversion macros and provided corresponding classes. It has three unified forms:

Among them, the first c Represents a "class", so that the ATL 3.0 macro is different, the second C represents a constant, 2 represents a "to", and ex represents a buffer of a certain size. Sourcetype and destinationtype Can Be A, T, W, and OLE. The meanings are ANSI, Unicode, General, and Ole strings. For example, ca2ct converts ansi to a String constant of the normal type. The following is some sample code:

Lptstr tstr = ca2tex <16> ("this is a test ");

Lpctstr tcstr = ca2ct ("this is a test ");

Wchar_t wszstr [] = l "this is a test ";

Char * chstr = cw2a (wszstr );

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.