zoukankan      html  css  js  c++  java
  • CString-int-string-char-BSTR之间的转换

    一.CString, int, string, char*之间的转换

    string CString

    CString.Format("%s", string.c_str());
    char CString
      CString.Format("%s", char*);
    char string
      string s(char *);
    string char *
      char *p = string.c_str();
    CString string
      string s(CString.GetBuffer());


    1.string -> CString
      CString. Format("%s", string.c_str());
      用c_str()确实比data()要好.
    2.char -> string
      string s(char *);
      你的只能初始化,在不是初始化的地方最好还是用assign().
    3.CString -> string
      string s(CString.GetBuffer());
      GetBuffer()后一定要ReleaseBuffer(),否则就没有释放缓冲区所占的空间.

    C++标准函数库》中:
    有三个函数可以将字符串的内容转换为字符数组和C—string
    1.data(),返回没有”“的字符串数组
    2.c_str(),返回有”“的字符串数组
    3.copy()

    CString 和 int互转
    将字符转换为整数,可以使用atoi_atoi64atol.

    将数字转换为CString变量,可以使用CStringFormat函数。如

    1CString s;
    int i = 64;
    s.Format("%d", i)

    Format函数的功能很强,值得你研究一下。

    2void CStrDlg::OnButton1()
    {
    // TODO: Add your control notification handler code here
    CString ss="1212.12";
    int temp=atoi(ss);              //字符转换为整数
    CString aa;
    aa.Format("%d",temp);         //整数转换为字符
    AfxMessageBox("var is " + aa);
    }

    CString和char*互转

    1char * -> CString
    CString strtest;
    char * charpoint;
    charpoint="give string a value";
    strtest=charpoint;

    2CString -> char *
    charpoint=strtest.GetBuffer(strtest.GetLength());
    标准C里没有string,char *==char []==string

    可以用CString.Format("%s",char *)这个方法来将char *转成CString

    要把CString转成char *,用操作符(LPCSTRCString就可以了。

    CString-> char[100]
    char a[100];
    CString str("aaaaaa");
    strncpy(a,(LPCTSTR)str,sizeof(a));

    CString类型的转换成int

    1CString类型的转换成int,可以使用atoi_atoi64atol
    例:CString aaa = "16" ;
        int int_chage = atoi((lpcstr)aaa) ;

    2将数字转换为CString变量,可以使用CStringFormat函数。

    例:CString s;
    int i = 64;
    s.Format("%d", i)

    CString ss="1212.12";
    int temp=atoi(ss);
    CString aa;
    aa.Format("%d",temp);

    如果是使用char数组,也可以使用sprintf函数

    数字->字符串除了用CString::Format,还有FormatVsprintf和不需要借助于Afxitoa

    string->char*

    string aa("aaa");

    char *c=aa.c_str();

    注:1.string.c_str()只能转换成const char *:const char *c=aa.c_str();

    2.cannot convert from 'const char *' to 'char *'
    3.要转成char *这样写:
     string mngName
     char t[200];

    memset(t,0,200);

    strcpy(t,mngName.c_str());

    BSTR转换成char*
    方法一:使用ConvertBSTRToString。例如:
      #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);            // 用完释放
        delete[] lpszText2;
         return 0;
       }

    方法二:使用_bstr_t的赋值运算符重载。例如:
    _bstr_t b = bstrText;
    char* lpszText2 = b;

    char*转换成BSTR

    方法一:使用SysAllocStringAPI函数。例如:
     BSTR bstrText = ::SysAllocString(L"Test");
     BSTR bstrText = ::SysAllocStringLen(L"Test",4);
     BSTR bstrText = ::SysAllocStringByteLen("Test",4);

    方法二:使用COleVariant_variant_t。例如:
    COleVariant strVar("This is a test");
    _variant_t strVar("This is a test");
    BSTR bstrText = strVar.bstrVal;

    方法三,使用_bstr_t,这是一种最简单的方法。例如:
    BSTR bstrText = _bstr_t("This is a test");

    方法四,使用CComBSTR。例如:
    BSTR bstrText = CComBSTR("This is a test");

    CComBSTR bstr("This is a test");
    BSTR bstrText = bstr.m_str;

    方法五,使用ConvertStringToBSTR。例如:
    char* lpszText = "Test";
    BSTR bstrText = _com_util::ConvertStringToBSTR(lpszText);

    CString转换成BSTR
    通常是通过使用CStringT::AllocSysString来实现。例如:
    CString str("This is a test");
    BSTR bstrText = str.AllocSysString();

    SysFreeString(bstrText); // 用完释放

    BSTR转换成CString
    一般可按下列方法进行:
    BSTR bstrText = ::SysAllocString(L"Test");
    CStringA str;
    str.Empty();
    str = bstrText;

    CStringA str(bstrText);

    ANSIUnicode和宽字符之间的转换
    方法一:使用MultiByteToWideCharANSI字符转换成Unicode字符,

    使用WideCharToMultiByteUnicode字符转换成ANSI字符。
    方法二:使用“_T”ANSI转换成一般类型字符串,使用“L”ANSI转换成Unicode

    在托管C++环境中还可使用SANSI字符串转换成String*对象。

    例如:TCHAR tstr[] = _T("this is a test");
    wchar_t wszStr[] = L"This is a test";
    String* str = S”This is a test”;

    方法三:使用ATL 7.0的转换宏和类。

    ATL7.0在原有3.0基础上完善和增加了许多字符串转换宏以及提供相应的类

    其中,第一个C表示,以便于ATL 3.0宏相区别,第二个C表示常量,2表示“to”EX表示要开辟一定大小的缓冲。SourceTypeDestinationType可以是ATWOLE,其含义分别是ANSIUnicode一般类型和OLE字符串。

    例如:CA2CT就是将ANSI转换成一般类型的字符串常量。下面是一些示例代码:
    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);

    二.VC字符串转换(BSTR CString)

     

    一.BSTRLPSTRLPWSTR

    Visual C++.NET的所有编程方式中,我们常常要用到这样的一些基本字符串类型,如BSTRLPSTRLPWSTR等。之所以出现类似上述的这些数据类型,是因为不同编程语言之间的数据交换以及对ANSIUnicode和多字节字符集(MBCS)的支持。

    那么什么是BSTRLPSTR以及LPWSTR呢?

    1BSTR(Basic STRingBasic字符串)是一个OLECHAR*类型的Unicode字符串。它被描述成一个与自动化相兼容的类型。由于操作系统提供相应的API函数(SysAllocString)来管理它以及一些默认的调度代码,因此BSTR实际上就是一个COM字符串,但它却在自动化技术以外的多种场合下得到广泛使用。

    2LPSTRLPWSTRWin32VC++所使用的一种字符串数据类型。LPSTR被定义成是一个指向以NULL(‘’)结尾的8ANSI字符数组指针,而LPWSTR是一个指向以NULL结尾的16位双字节字符数组指针。在VC++中,还有类似的字符串类型,如LPTSTRLPCTSTR等。

    例如,LPCTSTR是指“long pointer to a constant generic string”,表示一个指向一般字符串常量的长指针类型,与C/C++const char*相映射,而LPTSTR映射为 char*。

    一般地,还有下列类型定义:

    #ifdef UNICODE

      typedef LPWSTR LPTSTR;

      typedef LPCWSTR LPCTSTR;

    #else

      typedef LPSTR LPTSTR;

      typedef LPCSTR LPCTSTR;

    #endif

    二.CStringCStringA CStringW

    Visual C++.NET中将CStringT作为ATLMFC的共享的一般字符串类,它有CStringCStringACStringW三种形式,分别操作不同字符类型的字符串。这些字符类型是TCHARcharwchar_tTCHARUnicode平台中等同于WCHAR(16Unicode字符),在ANSI中等价于charwchar_t通常定义为unsigned short。由于CStringMFC应用程序中经常用到,这里不再重复。

    三、VARIANTCOleVariant _variant_t

    OLEActiveXCOM中,VARIANT数据类型提供了一种非常有效的机制,由于它既包含了数据本身,也包含了数据的类型,因而它可以实现各种不同的自动化数据的传输。下面让我们来看看OAIDL.H文件中VARIANT定义的一个简化版:

    struct tagVARIANT {

      VARTYPE vt;

      union {

       short iVal; // VT_I2.

       long lVal; // VT_I4.

       float fltVal; // VT_R4.

       double dblVal; // VT_R8.

       DATE date; // VT_DATE.

       BSTR bstrVal; // VT_BSTR.

      

       short * piVal; // VT_BYREF|VT_I2.

       long * plVal; // VT_BYREF|VT_I4.

       float * pfltVal; // VT_BYREF|VT_R4.

       double * pdblVal; // VT_BYREF|VT_R8.

       DATE * pdate; // VT_BYREF|VT_DATE.

       BSTR * pbstrVal; // VT_BYREF|VT_BSTR.

      };

    };

    显然,VARIANT类型是一个C结构,它包含了一个类型成员vt、一些保留字节以及一个大的union类型。例如,如果vtVT_I2,那么我们可以从iVal中读出VARIANT的值。同样,当给一个VARIANT变量赋值时,也要先指明其类型。例如:

    VARIANT va;

    :: VariantInit(&va); // 初始化

    int a = 2002;

    va.vt = VT_I4; // 指明long数据类型

    va.lVal = a; // 赋值

    为了方便处理VARIANT类型的变量,Windows还提供了这样一些非常有用的函数:

      VariantInit —— 将变量初始化为VT_EMPTY

      VariantClear —— 消除并初始化VARIANT

      VariantChangeType —— 改变VARIANT的类型;

      VariantCopy —— 释放与目标VARIANT相连的内存并复制源VARIANT

      COleVariant类是对VARIANT结构的封装。它的构造函数具有极为强大大的功能,当对象构造时首先调用VariantInit进行初始化,然后根据参数中的标准类型调用相应的构造函数,并使用VariantCopy进行转换赋值操作,当VARIANT对象不在有效范围时,它的析构函数就会被自动调用,由于析构函数调用了VariantClear,因而相应的内存就会被自动清除。除此之外,COleVariant的赋值操作符在与VARIANT类型转换中为我们提供极大的方便。例如下面的代码:

    COleVariant v1("This is a test"); // 直接构造

    COleVariant v2 = "This is a test";

    // 结果是VT_BSTR类型,值为"This is a test"

    COleVariant v3((long)2002);

    COleVariant v4 = (long)2002;

    // 结果是VT_I4类型,值为2002

    _variant_t是一个用于COMVARIANT类,它的功能与COleVariant相似。不过在Visual C++.NETMFC应用程序中使用时需要在代码文件前面添加下列两句:

      #include "comutil.h"

      #pragma comment( lib, "comsupp.lib" )

    四.CComBSTR_bstr_t

    CComBSTR是对BSTR数据类型封装的一个ATL类,它的操作比较方便。例如:

    CComBSTR bstr1;

    bstr1 = "Bye"; // 直接赋值

    OLECHAR* str = OLESTR("ta ta");    // 长度为5的宽字符

    CComBSTR bstr2(wcslen(str)) ;       // 定义长度为5

    wcscpy(bstr2.m_str, str);             // 将宽字符串复制到BSTR

    CComBSTR bstr3(5, OLESTR("Hello World"));

    CComBSTR bstr4(5, "Hello World");

    CComBSTR bstr5(OLESTR("Hey there"));

    CComBSTR bstr6("Hey there");

    CComBSTR bstr7(bstr6);             // 构造时复制,内容为"Hey there"

    _bstr_t是是C++BSTR的封装,它的构造和析构函数分别调用SysAllocStringSysFreeString函数,其他操作是借用BSTR API函数。与_variant_t相似,使用时也要添加comutil.hcomsupp.lib

    五.BSTRchar*CString转换

    (1) char*转换成CString

    若将char*转换成CString,除了直接赋值外,还可使用CString::Format进行。例如:

    char chArray[] = "This is a test";

    char * p = "This is a test";

    或:LPSTR p = "This is a test";

    或在已定义Unicode应的用程序中:TCHAR * p = _T("This is a test");

    或:LPTSTR p = _T("This is a test");

    CString theString = chArray;

    theString.Format(_T("%s"), chArray);

    theString = p;

    (2) CString转换成char*

    若将CString类转换成char*(LPSTR)类型,常常使用下列三种方法:

    方法一.使用强制转换。例如:

    CString theString( "This is a test" );

    LPTSTR lpsz =(LPTSTR)(LPCTSTR)theString;

    方法二.使用strcpy。例如:

    CString theString( "This is a test" );

    LPTSTR lpsz = new TCHAR[theString.GetLength()+1];

    _tcscpy(lpsz, theString);

     需要说明的是,strcpy(或可移值Unicode/MBCS_tcscpy)的第二个参数是 const wchar_t* (Unicode)const char* (ANSI),系统编译器将会自动对其进行转换。

    方法三.使用CString::GetBuffer。例如:

    CString s(_T("This is a test "));

    LPTSTR p = s.GetBuffer();      // 在这里添加使用p的代码

    if(p != NULL) *p = _T('');

    s.ReleaseBuffer();      // 使用完后及时释放,以便能使用其它的CString成员函数

    (3) BSTR转换成char*

    方法一.使用ConvertBSTRToString。例如:

    #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); // 用完释放

    delete[] lpszText2;

    return 0;

    }

    方法二.使用_bstr_t的赋值运算符重载。例如:

    _bstr_t b = bstrText;

    char* lpszText2 = b;

    (4) char*转换成BSTR

    方法一.使用SysAllocStringAPI函数。例如:

    BSTR bstrText = ::SysAllocString(L"Test");

    BSTR bstrText = ::SysAllocStringLen(L"Test",4);

    BSTR bstrText = ::SysAllocStringByteLen("Test",4);

    方法二.使用COleVariant_variant_t。例如:

    //COleVariant strVar("This is a test");

    _variant_t strVar("This is a test");

    BSTR bstrText = strVar.bstrVal;

    方法三.使用_bstr_t,这是一种最简单的方法。例如:

    BSTR bstrText = _bstr_t("This is a test");

    方法四.使用CComBSTR。例如:

    BSTR bstrText = CComBSTR("This is a test");

    CComBSTR bstr("This is a test");

    BSTR bstrText = bstr.m_str;

    方法五.使用ConvertStringToBSTR。例如:

    char* lpszText = "Test";

    BSTR bstrText = _com_util::ConvertStringToBSTR(lpszText);

    (5) CString转换成BSTR

    通常是通过使用CStringT::AllocSysString来实现。例如:

    CString str("This is a test");

    BSTR bstrText = str.AllocSysString();

    SysFreeString(bstrText); // 用完释放

    (6) BSTR转换成CString

    一般可按下列方法进行:

    BSTR bstrText = ::SysAllocString(L"Test");

    CStringA str;

    str.Empty();

    str = bstrText;

    CStringA str(bstrText);

    (7) ANSIUnicode和宽字符之间的转换

    方法一.使用MultiByteToWideCharANSI字符转换成Unicode字符,使用WideCharToMultiByteUnicode字符转换成ANSI字符。

    方法二.使用“_T”ANSI转换成一般类型字符串,使用“L”ANSI转换成Unicode,而在托管C++环境中还可使用SANSI字符串转换成String*对象。例如:

    TCHAR tstr[] = _T("this is a test");

    wchar_t wszStr[] = L"This is a test";

    String* str = S”This is a test”;

    方法三:使用ATL 7.0的转换宏和类。ATL7.0在原有3.0基础上完善和增加了许多字符串转换宏以及提供相应的类

    其中,第一个C表示,以便于ATL 3.0宏相区别,第二个C表示常量,2表示“to”EX表示要开辟一定大小的缓冲。SourceTypeDestinationType可以是ATWOLE,其含义分别是ANSIUnicode一般类型和OLE字符串。

    例如:CA2CT就是将ANSI转换成一般类型的字符串常量。下面是一些示例代码:
           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);

    三.CStringBSTRLPTSTR之间的区别

    一.定义

    1CString:动态的TCHAR数组。它是一个完全独立的类,封装了+等操作符和字符串操作方法。

    2.BSTR:专有格式的字符串(需要使用系统函数来操纵)。

    定义为:typedef OLECHAR FAR* BSTR

    3.LPCTSTR:常量的TCHAR指针。定义为:typedef const char* LPCTSTR

    二.要点

    1.char*:指向ANSI字符数组的指针,其中每个字符占8位(有效数据是除掉最高位的其他七位),它保持了与传统C/C++的兼容。

    2.LPSTR:指向一个以“”结尾的ANSI字符数组的指针,可与char*互换使用,它通常在Win32中使用。其中LP表示长指针(long pointer)。

    3.LPCSTR:该数据类型的特性在于它的实例不能被使用它的API函数改变,除此之外与LPSTR等同。其中C表示常量(CONSTANT)。

    4.在Win16下长指针(LP)和短指针(P)有区别,而在Win32下它们是没有区别的,都是32位。

    5.TCHAR在采用Unicode方式下编译时为wchar_t,在普通编码方式下编译时位char

    三.Unicode标准

    1.为了满足程序代码国际化的需要,业界推出了Unicode标准,它提供了一种简单和一致的表示字符串的方法,所有字符中的字节都是16位(两个字节)的值,其数量也可以满足几乎世界上所有书面语言字符的编码需求,开发程序时使用Unicode(类型wchar_t)是一种被鼓励的做法。

    2.LPWSTRLPCWSTR由此产生,它们的含义类似于LPSTRLPCSTR,不同的是字符数据wchar_t16位,而char却为8位。

    四.TCHAR数据类型

    TCHAR数据类型是为了实现ANSIUnicode两种编码的通用而提出来的

    1.如果定义了_UNICODE,则声明如下:

        typedef wchar_t TCHAR;

    2.如果没有定义_UNICODE,则声明如下:

        typedef char TCHAR;

    这样就可以让CStringLPTSTRLPCTSTR中的每个字符都是TCHAR类型,而不考虑它们的编码格式。而且CString是一个封装好了的类,更是大大地方便了用户的使用。

    五、VC++中常用数据类型之间的转换

    1.定义

        int i=100;

        long l=2001;

        float f=300.2;

        double d=12345.119

        char username[]="2008北京奥运";

        char temp[200];

        char* buf;

        CString str;

        _variant_t v1;

        _bstr_t v2;

    2.其他数据类型到字符串的转换

    (1)短整形int->字符串

        itoa(i,temp,10);     //按十进制把i转换为字符串存入temp

        itoa(i,temp,2);      //按二进制把i转换为字符串存入temp

    (2)长整形long->字符串

        ltoa(l,temp,10);

    3.从其他包含了字符串的变量中获取指向该字符串的指针

    (1)CString变量中获取字符串

        str="祈福四川";

        buf=(LPSTR)(LPCTSTR)str;

    (2)BSTR类型的_varitant_t变量中获取字符串

        v1=(_bstr_t)"程序员";

        buf=_com_util::ConvertBSTRToString((_bstr_t)v1);

    4.字符串转换为其他数据类型

        strcpy(temp,"123");

    (1)i=atoi(temp);       //字符串->短整型int

    (2)l=atol(temp);       //字符串->长整形long

    (3)d=atof(temp);      //字符串->浮点型double

    5.其他数据类型转换到CString

    (1)使用CString的成员函数Format来转换

    Astr.Format("%d",i); //短整型int->CString

    Bstr.Format("%f",f); //浮点数float->CString

    (2)支持CString构造函数的数据类型可以直接赋值,例如char*

        str=username;

    六.BSTR_bstr_tCCombBSTR

    BSTR:指向字符串的32位指针,_bstr_tCComBSTR都是对它的封装。

    1.char*->BSTR的转换

        BSTR b=_com_util::ConvertStringToBSTR("数据");

        注:使用之前需要加上comutil.h头文件

    2.BSTR->char*的转换

        char* p=_com_util::ConvertBSTRToString(b);

    七.VARIANT_variant_tCOleVariant

    1.对于VARIANT变量的赋值:首先给vt成员赋值,指明数据类型。再对联合结构中相同数据类型的变量赋值(可参考VC98InludeOAIDL.H头文件中关于tagVARIANT结构体的定义)。举例如下:

       VARIANT va;

        va.vt=VT_l4;          //指明数据类型

        va.lVal=2008;

    2.对于不马上赋值的VARIANT,最好先使用void VariantInit(VARIANTARG FAR* pvarg)函数对其进行初始化,其本质是将vt设置为VT_EMPTYvt与常用数据类型的对应关系(略)。

    3.variant_tVARIANT的封装类,赋值可以使用强制类型转换,其构造函数会自动处理这些数据类型。

    例如: long l=222;

            int i=100;

            _variant_t lVal(l);

            lVal=(long)i;

    4.COleVariant_variant_t的使用方法基本一样,示例如下:

        COleVariant v3="字符串",v4=(long)1999;

        CString str=(BSTR)v3.pbstrVal;

        long l=v4.lVal;

    八.其他

    1.对消息的处理中,我们通常需要将WPARAMLPARAM32位数据(DWORD)分解成两个16位数据(WORD),例如:

        LPARAM lParam;

        WORD loValue=LOWORD(lParam); //取低16

        WORD hiValue=HIWORD(lParam); //取高16

    2.对于16位的数据(WORD),我们可以使用同样的方法分解成高低两个8位的数据(BYTE),例如:

        WORD wValue;  

        BYTE loValue=LOBYTE(wValue); //取低8

        BYTE hiValue=HIBYTE(wValue); //取高8

    3.如何将CString类型的变量赋给char*类型的变量

    (1)CString::GetBuffer函数

        char* p;

        CString str="hello";

        p=str.GetBuffer(str.GetLength());

        str.ReleaseBuffer();

    (2)strcpy函数

        CString str("aaaaaaaa");

        strcpy(str.GetBuffer(10),"aa");     //string->char

        str.ReleaseBuffer();

        GetBuffer(int n)函数用于获取字符数组,其中n表示字符数组的长度,使用完该字符数组之后一定要调用ReleaseBuffer()函数来释放这个字符数组。

        注:在能够使用const char*的地方,通常不要使用char*

    (3)memcpy函数

        CString mCS=_T("cxl");

        char mch[20];

        memcpy(mch,mCS,20);

    (4)LPCTSTR强制类型转换(不建议使用)

        char* ch;

        CString str;

        ch=(LPSTR)(LPCTSTR)str;

        str="good!";

        sprintf(ch,"%s",(LPTSTR)(LPCTSTR)str);

    (5)CString->LPTSTR->char*

        CString Msg;

        Msg=Msg+"abc";

        LPTSTR lpsz;

        lpsz=new TCHAR[Msg.GetLength()+1];

        _tcscpy(lpsz,Msg);

        char* psz;

        strcpy(psz,lpsz);

    4.如何将CString类型的变量赋给const char*类型的变量

        char* a[100];

        CString str("abcdef");

        strncpy(a,(LPCTSTR)str,sizeof(a));

       

        strncpy(a,str,sizeof(a));

        注:编译器会自动将CString类型的变量转换为const char*类型

    5.如何将CString类型的变量赋给LPCTSTR类型的变量

        CString cStr;

        const char* lpctStr=(LPCTSTR)cStr;    //允许将非常量地址赋给指向常量的指针

    //不允许将常量地址赋给非常量指针

    6.如何将LPCTSTR类型的变量赋给CString类型的变量

    (LPCTSTR= CString= const char*)

        LPCTSTR lpctStr;

        CString cStr=lpctStr;

    7.如何将char*类型的变量赋给CString类型的变量

    (1)直接赋值:CString myString="This is a test";

    (2)构造函数:CString s1("Tom");

    8.如何将CString类型的变量赋给char[](字符串)类型的变量

    (1)sprintf函数

        CString str="good!";

        char temp[200];

        sprintf(temp,"%s",(LPCSTR)str);

        注:强制类型转换(LPCSTR)str(LPTSTR)(LPCTSTR)str等同,使用的区别仅在于CString对象是变量还是常量。

    LPCTSTR表示const char*,它得到的字符串是不可写的!如果将其强制转换位LPTSTR(去掉const),是极为危险的!要得到char*,应该使用GetBufferGetBufferSetLength函数,用完之后再调用ReleaseBuffer函数。

    (LPCSTR)str=(LPTSTR)(LPCTSTR)str

    (2)strcpy函数

        CString str;

        char c[256];

        strcpy(c,str);

        str="Hello";

        strcpy((char*)&c,(LPCTSTR)str);

    九.关于CString的使用

    1.指定CString形参

    (1)对于大多数需要字符串参数的函数,最好将函数原型中的形参指定为一个指向字符(LPCTSTR),而非CStringconst指针。当将形参指定为指向字符的const指针时,可将指针传递到TCHAR数组(如字符串["hihere"]或传递到CString对象)。CString对象将自动转换成LPCTSTR。任何能够使用LPCTSTR的地方也能够使用CString对象。

    (2)如果某个形参将不会被修改,则也将该参数指定为常量字符串引用(const CString&)。如果函数要修改该字符串,则删除const修饰符。如果需要默认为空值,则将其初始化为空字符串([""]),如下所示:

        void AddCustomer(const CString& name,const CString& address,const CString& comment="");

    (3)对于大多数函数的结果,按值返回CString对象即可。

    2.串的基本运算

        char s1[20]="dir/bin/appl",s2[20]="file.asm",s3[30],*p;

        int result;

    (1)求串长

        int strlen(char* s);               //求串s的长度

        例:printf("%d",strlen(s1));

    (2)串复制

        char* strcpy(char* to,char* from);  //from串复制到to串中,并返回to开始处的指针

        例:strcpy(s3,s1);

    (3)串联接

        char* strcat(char* to,char* from);   //from串复制到to串的末尾

        例:strcat(s3,"/");

               strcat(s3,s2);

    (4)串比较

        int strcmp(char* s1,char* s2);      //比较s1s2的大小,s1<s2(小于0)、s1=s20)和s1>s2(大于0

        例:result=strcmp("baker","Baker"); //大于0

               result=strcmp("12","12");            //等于0

               result=strcmp("Joe","joseph");    //小于0

    (5)字符定位

        char* strchr(char* s,char c);    //c在字符串s中第一次出现的位置。若找到,则返回该位置;否则NULL

        例:p=strchr(s2,'.'); //p指向"file"之后的位置

    (6)注意

    A:上述操作是最基本的,其中后4个操作还有变种形式:strncpystrncathstrnchr

    B:其他的串操作见C<string.h>头文件。在不同的高级语言中,对串运算的种类及符号都不尽相同。

    C:其余的串操作一般可由这些基本操作组合而成。

        例:求子串的操作可如下实现

        //ssub是字符数组,用sub返回串s的第pos个字符长度为len的子串

        void substr(char* sub,char* s,int pos,int len)

        {

            //其中0<=pos<=strlen(s)-1,且数组sub至少可容纳len+1个字符

            if(pos<0 || pos>strlen(s)-1 ||len<0)

                Error("parameter error!");

             //s[pos]起复制至多len个字符到sub

             strncpy(sub,*s[pos],len);

        }

     

  • 相关阅读:
    qemu的几篇文章---涉及qemu的注入
    网络以及linux相关--重点参考--包含相关linux服务的部署和使用
    ipv6的相关情况简略说明
    向日葵服务器相关ip,通过ipset过滤(oray.com oray.net),可能不完整,需要dnsmasq ipset持续监听相关域名
    linux shell重要参考网站
    web信息收集分类
    针对管理员的信息收集 以及 它的意义
    目标网站弱点功能探测
    网站文件目录探测
    目标后台探测以及物理路径探测
  • 原文地址:https://www.cnblogs.com/ranjiewen/p/5444098.html
Copyright © 2011-2022 走看看