Conversion between cstring and lpcwstr, lpstr, char *, and lpwstr

Source: Internet
Author: User
Conversion between cstring and lpcwstr, lpstr, char *, and lpwstr

I. cstring and lpcwstr

The difference between the two: the lpcwstr is a unicode string pointer. When the initialization is performed, the request space is as large as the size of the string. If the storage space exceeds the value, unexpected results will appear, this is different from cstring. While cstring is a string class, the memory space class is automatically managed.

Convert cstring to lpcwstr

Method 1: cstring strfilename;

Lpcwstr = strfilename. allocsysstring ();

Method 2: cstring STR = _ T ("teststr ");
Uses_conversion;
Lpcwstr = a2cw (lpcstr) Str );

In MFC, cstring and lpstr can be used in a common sense. a2cw indicates (lpcstr)-> (lpcwstr), and user_conversion indicates that some intermediate variables are defined, you must define this statement before using the ATL conversion macro.

Return the string of the request to cstring.

Lpcwstr = l "testwstr ";
Cstring STR (lpcwstr );

Cstring STR;

Lpwstr lpstr = (lpwstr) (lpcwstr) STR;

Ii. cstring and lpstr Conversion

Cstring to lpstr:

Method 1: cstring strfilename;

Lpstr = strfilename. getbuffer ();

Strfilename. releasebuffer ();

Method 2: cstring strfilename;

Lpstr = (lpstr) (lpcstr) strfimename;

Lpstr to cstring:

Lpstr = l "teststr ";
Cstring STR (lpstr );

Note: cstring and lpcstr can be directly converted as follows:

Cstring STR;

Maid;

Iii. cstring and char * Conversion
Cstring to char *
Method 1: cstring STR;
Char * P = Str. getbuffer ();
Method 2: cstring STR;
Char * P = (lpstr) (lpcstr) STR;
Char * To cstring
Char * P = "test ";
Cstring STR = ("% s", P );

Iv. String, Int, float Conversion

You can use functions such as atoi, atof, and atol.

5. lpstr (char *) and lpwstr Conversion

You can use the following ATL macro to define variables as tchar, lptstr, and other T types to avoid conversion.

ATL macro introduction:

A2bstr ole2a T2A w2a
A2cole ole2bstr t2bstr w2bstr
A2ct ole2ca t2ca w2ca
A2cw ole2ct t2cole w2cole
A2ole ole2cw t2cw w2ct
A2t ole2t t2ole w2ole
A2w ole2w T2W w2t

A: ANSI string, that is, MBCS.
W, Ole width string, that is, Unicode.
T intermediate type T. If _ Unicode is defined, t indicates W. If _ MBCS is defined, t indicates.
C const

Using these macros, you can quickly convert various characters. Before use, the header file must be included and user_conversion must be affirmed. If you use ATL to convert macros, It is very convenient to use because you do not need to release temporary space. But considering the size of the stack space (2 MB by default for VC), pay attention to the following points during use:

1. It is only suitable for converting short strings;
2. Do not try to convert in a loop with a large number of times;
3. Do not convert the content of a character-type file because the file size is usually large;
4. For situations 2 and 3, use multibytetowidechar () and widechartomultibyte ();

Void func1 (lpstr );

Void func2 (lpwstr );

Tchar name [256];

Tchar * pname = new tchar [256];

Func1 (name); // func1 (pname );

Func2 (name); // func2 (pname );

Note that the Code marked with red in vs2005 is no longer valid.

In vs2005, The cstring type has been changed to the wide struct type. Some conversions are as follows:

Char name [10];
Tchar sex [5];
Char * P = Name;
Tchar * PW = sex;

Lpstr = Name;
Lpcstr = Name;
Lpcstr = lpstr;
 
Lpstr = P;
P = (char *) sex;
PW = (wchar *) Name;
Lpwstr = (lpwstr) lpstr;
Lpwstr = (lpwstr) lpcstr;
Lpcwstr = (lpcwstr) lpstr;
Maid name;

Cstring STR (lpstr );
Cstring str1 (lpcstr );
Cstring str2 (lpwstr );
Cstring str3 (lpcwstr );
Cstring str4 (name );
Cstring str5 (sex );

Lpwstr = (lpwstr) (lpcwstr) STR;
Lpstr = (lpstr) (lpcwstr) STR;
Maid STR;

P = (char *) Str. getbuffer ();
PW = Str. getbuffer ();

It can be seen that the conversion is simpler. Basically, it can be directly converted. a2w and other macros are not needed.

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.