Character (string) pointer in C + +

Source: Internet
Author: User
Tags ole

(a) Char wchar_t (WCHAR) Tcharansi:char is a data type of 8-bit ANSI characters that can be used in string processing functions: strcat (), strcpy (), strlen (), and so on. unicode:wchar_t is a data type of 16-bit Unicode characters and can be used as a string handler function: Wcscat (), wcscpy (), wcslen (), and other functions that begin with WCS. It is actually defined in: typedef unsigned short wchar_t. In the header file there is such a definition: typedef wchar_t WCHAR; So WCHAR is actually wchar_t. 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." The TCHAR in vc++:vc++ represent Char and wchar_t (WCHAR) in the ANSI and Unicode environments, respectively. Defined as: #ifdef UNICODE
typedef wchar_t TCHAR;
#else
typedef char TCHAR;;
#endif (ii) LPSTR LPCSTR lpwstr lpcwstr LPTSTR lpctstransi:lpstr, LPCSTR is a string pointer type of ANSI character, respectively, is a normal character strings, a constant string representing "long pointer to a generic string", "long pointer to a constant generic string". is a pointer to an array of 8-bit ANSI characters ending with null (' Unicode:lpwstr '), a string pointer type of LPCWSTR that is a Unicode character, a normal double-byte string, a constant double-byte string, respectively, representing the "long Pointer to a generic DWORD string ', ' long pointer to a constant generic DWORD string '. is a pointer to an array of 16-bit Unicode characters ending with null (' s ').when usingstring Constantsyou need to use _text ("MyStr") or _t ("") to support the automatic conversion of the system. The LPTSTR and LPCTSTR in vc++:vc++ represent LPStr, LPCSTR, and LPWStr, respectively, in the ANSI and Unicode environments. Defined as: #ifdef UNICODE
typedef LPWSTR LPTSTR;
typedef LPCWSTR LPCTSTR;
#else
typedef LPSTR LPTSTR;
typedef LPCSTR LPCTSTR;
#endif (c) Reciprocal conversion

(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) 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 in the original 3.0 based on the perfect and add a lot of string conversion macros and provide the corresponding classes, it is strongly recommended to use the ALT macro conversion, very convenient:

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, W, T, and OLE, meaning ANSI, Unicode, generic, and OLE strings, respectively. For example, CA2CT is a string constant that converts ANSI to a generic type: In order to use these macros, you must initialize some local variables with uses_conversion at the beginning of the function. Here are some sample code:

Uses_conversion;

W2A (CString); Converts the Unicode CString to char * return.

A2W (char *); convert char * to Unicode WCHAR return.

T2A,A2T T represents w in the Unicode environment, and T represents a in the ANSI environment.

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

Character (string) pointer in C + +

Related Article

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.