Summary: An in-depth study on data type conversion of BSTR, char *, and cstring in VC. STEP/Method
-
- 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 Unicode applications that have been definedProgramMedium
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;
- cstring to char *
If you convert the cstring class 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. Example:
cstring thestring ("this is a test");
lptstr lpsz = new tchar [thestring. getlength () + 1];
_ tcscpy (lpsz, thestring);
note that strcpy (or _ tcscpy of Unicode/MBCS) the second parameter is const wchar_t * (UNICODE) or const char * (ANSI). The system compiler will automatically convert it.
method 3: Use cstring: getbuffer. Example:
cstring S (_ T ("this is a test");
lptstr P = S. getbuffer ();
// Add the Code
If (P! = NULL) * P = _ T ('\ 0');
S. releasebuffer ();
// release immediately after use, so that other cstring member functions can be used
- convert BSTR to char *
Method 1: Use convertbstrtostring. Example:
# include "comutil. H "
# 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;
}< br> ** this method is not good and may cause memory leakage, and sysfreestring has no effect.
Method 2: Use the _ bstr_t value assignment operator to overload it. Example:
_ bstr_t B = bstrtext;
char * lpsztext2 = B;
* No Memory leakage. recommended method
- char * is converted to BSTR
method 1. Use APIs such as sysallocstring. 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. Example:
char * lpsztext = "test";
BSTR bstrtext = _ com_util: convertstringtobstr (lpsztext);
(5) cstring to BSTR
it is usually implemented by using cstringt: allocsysstring. Example:
cstring STR ("this is a test");
BSTR bstrtext = Str. allocsysstring ();
...
sysfreestring (bstrtext); // release after use
- 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);
- conversion between ANSI, Unicode, and wide characters
method 1, use multibytetowidechar to convert ANSI characters to unicode characters, and use widechartomultibyte to convert Unicode characters to ANSI characters.
Method 2: Use "_ t" to convert ANSI to a "normal" 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. 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 String Conversion macros and provided corresponding classes.
in this example, the first c Represents a "class", so that the ATL 3.0 macro is different. The second C represents a constant, and 2 represents a "to" macro ", ex indicates opening up 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);