Data type conversion in VC: BSTR, char *, and cstring

Source: Internet
Author: User
Summary: An in-depth study on data type conversion of BSTR, char *, and cstring in VC. STEP/Method
  1. 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;
  2. 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
  3. 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
  4. 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
  5. 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);
  6. 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);

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.