C + + Character types Summary difference Wchar_t,char,wchar

Source: Internet
Author: User
Tags ole

Go to: http://www.360doc.com/content/12/0807/01/9290626_228750141.shtml

1. Distinguishing Wchar_t,char,wchar

ANSI: Char, a string handler function is available: strcat (), strcpy (), strlen (), and other functions that begin with Str.
unicode:wchar_t is the data type of the Unicode character, which is actually defined in:
typedef unsigned short wchar_t;
In addition, there is such a definition in the header file: typedef wchar_t WCHAR; So WCHAR is actually wchar_t.
wchar_t can be used with string handlers: Wcscat (), wcscpy (), wcslen (), and other functions that begin with WCS. In order for the compiler to recognize Unicode strings, it must be preceded by an "L", for example: wchar_t *sztest=l "This is a Unicode string.";

2.TCHAR

The

provides _UNICODE macros (underlined) in the C language, provides Unicode macros (without underscores) in Windows, and automatically switches to the Unicode version if _unicode macros and Unicode macros are set, otherwise The system compiles and runs in ANSI manner. Only macros are defined and cannot be converted automatically, and a series of character definition support is required.
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;
   Description: You need to use _text ("MyStr") or _t ("") to support automatic conversion of the system when using string constants.

3.BSTR

A BSTR is a string with a length prefix, which is primarily managed by the operating system, so use the API. Mainly used to deal with VB (VB string refers to it) to operate its API function has a lot. Like sysallocstring,sysfreestring and so on. .
VC encapsulates its classes such as _bstr_t, and ATL in CComBSTR and so on.
A BSTR consists of a header and a string containing the length information of the string, which can contain embedded null values.
A BSTR is passed in the form of a pointer. (A pointer is a variable that contains the memory address of another variable, not the data.) The BSTR is Unicode, which requires two bytes per character. A BSTR usually ends with a two-byte null character. WSTR is a wide character, in double-byte notation, a character BSTR is intended to be compatible with the original basic character, and its first 4 bytes are its length, ending with '/'.

4. A further string and the type definition of its pointer

Because the function list of the Win32 API document uses the usual name of the function (for example, "SetWindowText"), all strings are defined with TCHAR. (except for Unicode-only APIs introduced in XP). Here are some common typedefs that you can see on MSDN.

Type Meaning in MBCS builds Meaning in Unicode builds
WCHAR wchar_t wchar_t
LPSTR char* char*
LPCSTR Const char* Const char*
LPWStr wchar_t* wchar_t*
Lpcwstr wchar_t* wchar_t*
TCHAR TCHAR Char wchar_t
LPTSTR tchar* tchar*
Lpctstr Const tchar* Const tchar*



5. Converting to each other

(1) char* converted into CString
If you convert char* to CString, you can use Cstring::format in addition to directly assigning values. For example:
Char charray[] = "This is a test";
char * p = "This is a test";
Or
LPSTR p = "This is a test";
Or in a program in which Unicode is 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) CString converted into char*
If you convert the CString class to a char* (LPSTR) type, you often use the following three methods:
Method one, using casts. For example:
CString TheString ("This is a Test");
LPTSTR lpsz = (LPTSTR) (LPCTSTR) thestring;
Method two, use strcpy. For example:
CString TheString ("This is a Test");
LPTSTR lpsz = new Tchar[thestring.getlength () +1];
_tcscpy (Lpsz, thestring);
It should be explained that the second parameter of the strcpy (or Unicode/mbcs _tcscpy) is const wchar_t* (Unicode) or const char* (ANSI), which is automatically converted by the system compiler.
Method three, use Cstring::getbuffer. For example:
CString s (_t ("This is a Test");
LPTSTR p = s.getbuffer ();
Add the code that uses p here
if (p = NULL) *p = _t (' n ');
S.releasebuffer ();
Release immediately after use so that other CString member functions can be used
(3) A BSTR converted into char*
Method one, 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); Run out of release
Delete[] LPSZTEXT2;
return 0;
}
Method Two, use the _bstr_t assignment operator overload. For example:
_bstr_t B = bstrtext;
char* lpszText2 = b;
(4) char* converted into a BSTR
Method One, 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 Two, 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 three, using _bstr_t, is one of the simplest methods. For example:
BSTR Bstrtext = _bstr_t ("This is a Test");
Method four, use CComBSTR. For example:
BSTR Bstrtext = CComBSTR ("This is a Test");
Or
CComBSTR BSTR ("This is a Test");
BSTR bstrtext = bstr.m_str;
Method five, use Convertstringtobstr. For example:
char* lpszText = "Test";
BSTR Bstrtext = _com_util::convertstringtobstr (LpszText);
(5) CString converted into a BSTR
This is usually done by using cstringt::allocsysstring. For example:
CString Str ("This is a Test");
BSTR bstrtext = str. AllocSysString ();
...
SysFreeString (Bstrtext); Run out of release
(6) A BSTR converted into CString
Generally, the following methods can be used:
BSTR Bstrtext =:: SysAllocString (L "Test");
CStringA str;
Str. Empty ();
str = Bstrtext;
Or
CStringA str (bstrtext);
(7) Conversion between ANSI, Unicode, and wide characters
Method One, use MultiByteToWideChar to convert ANSI characters to Unicode characters, and use WideCharToMultiByte to convert Unicode characters to ANSI characters.
Method Two, use "_t" to convert ANSI to "generic" type string, use "L" to convert ANSI to Unicode, and in Managed C + + environment can also use S to convert ANSI string to 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 Three, use the conversion macros and classes of ATL 7.0. ATL7.0 has perfected and added many string conversion macros and provided the corresponding classes based on the original 3.0, which has a uniform form as shown in 3:
Where the first C denotes "class", so that the ATL 3.0 macros are distinguished, the second C represents a constant, 2 means "to", and ex indicates a buffer of a certain size. SourceType and destinationtype can be a, T, W, and OLE, meaning ANSI, Unicode, generic, and OLE strings, respectively. For example, CA2CT is a string constant that converts ANSI to a generic type. Here are 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);

C + + Character types Summary difference Wchar_t,char,wchar

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.