CString conversion from other types

Source: Internet
Author: User

 

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

 

8. Convert Cstring to char *

Cstring strFileName;

Int length = strFileName. GetLength ();

Char * cFileName;

CFileName = (char *) strFileName. GetBuffer (length );

 

9. Convert Cstring to LPSTR

Cstring str;

LPSTR lpstr = (LPSTR) str;

 

10. Convert Cstring to LPCSTR

Int length = strFileName. GetLength ();

Size_t aLen = length + 1;

 

LPCSTR lpstrFileName = (LPCSTR) strFileName. GetBuffer (length );

Int length = strFileName. GetLength ();

Size_t aLen = length + 1;

 

11. Convert Cstring to LPOLESTR/LPCOLESTR

Int length = strFileName. GetLength ();

Size_t aLen = length + 1;

LPCSTR lpstrFileName = (LPCSTR) strFileName. GetBuffer (length );

Int wLen = MultiByteToWideChar (CP_ACP, 0, lpstrFileName, aLen, NULL, 0 );

LPOLESTR lpFileName = new WCHAR [wLen];

MultiByteToWideChar (CP_ACP, 0, lpstrFileName, aLen, lpFileName, wLen );

 

LPOLESTR can be used as an LPCOLESTR input function. For example, you can use lpFileName (LPOLESTR) in HRESULT SetOutputFileName (const GUID * pType, LPCOLESTR lpstrFile, IBaseFilter ** ppf, limit ** ppSink)

 

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.