数据类型转换

1.基本概念 LPCSTR 32-bit指针,指向一个常数字符串 LPSTR 32-bit指针,指向一个字符串 LPCTSTR 32-bit指针,指向一个常数字符串,此字符串可移植到Unicode和DBCS(双字节字集) LPTSTR 32-bit指针,指向一个字符串。此字符串可移植到Unicode和DBCS(双字节字集) BSTR 一个OLECHAR*类型的Unicode字符串, com字符串 _bstr_t encapsulates the BSTR data type。The class manages resource allocation and deallocation through function calls to SysAllocString and SysFreeString and other BSTR APIs when appropriate CcomBSTR A wrapper for BSTRs, which are length-prefixed strings.The length is stored as an integer at the memory location preceding the data in the string. VARIANT 一个C结构,它包含了一个类型成员vt、一些保留字节以及一个大的union类型 _variant_t encapsulates the VARIANT data type。The class manages resource allocation and deallocation and makes function calls to VariantInit and VariantClear as appropriate. CString 一种很特殊的 C++ 对象,它里面包含了三个值:一个指向某个数据缓冲区的指针、一个是该缓冲中有效的字符记数(它是不可存取的,是位于 CString 地址之下的一个隐藏区域)以及一个缓冲区长度。有效字符数的大小可以是从0到该缓冲最大长度值减1之间的任何数(因为字符串结尾有一个NULL字符)。 2.处理VARIANT的windows API   VariantInit —— 将变量初始化为VT_EMPTY;   VariantClear —— 消除并初始化VARIANT;   VariantChangeType —— 改变VARIANT的类型;   VariantCopy —— 释放与目标VARIANT相连的内存并复制源VARIANT。 3.BSTR、char*和CString转换    ●char*转换成CString    将char*转换成CString,除了直接赋值外,还可使用CString::Format进行。    ●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);      ■使用CString::GetBuffer      CString s(_T("This is a test "));      LPTSTR p = s.GetBuffer();      // 在这里添加使用p的代码      if(p != NULL) *p = _T('/0');      s.ReleaseBuffer();// 使用完后及时释放,以便能使用其它的CString成员函数    ●BSTR转换成char*    使用_bstr_t的赋值运算符重载    BSTR bstrText = ::SysAllocString(L"Test");    _bstr_t b = bstrText;    char* lpszText2 = b;    SysFreeString(bstrText); // 用完释放    ●Char*转换为BSTR      ■使用SysAllocString等API函数      BSTR bstrText = ::SysAllocString(L"Test");      BSTR bstrText = ::SysAllocStringLen(L"Test",4);      BSTR bstrText = ::SysAllocStringByteLen("Test",4);      ■使用_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");    _bstr_t b = bstrText;    CString szStr = b;    SysFreeString(bstrText);    ●ANSI、Unicode和宽字符之间的转换      ■使用MultiByteToWideChar将ANSI字符转换成Unicode字符,使用WideCharToMultiByte将Unicode字符转换成ANSI字符。        ■使用“_T”将ANSI转换成“一般”类型字符串,使用“L”将ANSI转换成Unicode,而在托管C++环境中还可使用S将ANSI字符串转换成String*对象。例如:      TCHAR tstr[] = _T("this is a test");      wchar_t wszStr[] = L"This is a test";      String* str = S”This is a test”;    ●Cstring转化为int、long、Double      ■Cstring->int      int atoi( const char *str );      Cstring str = " -2309 ";      Int value = _ttoi( str );      ■Cstring->long      long atol(const char *str );      CString str = "3336402735171707160320";      Long value = atol( str );      ■Cstring->double      int _atodbl( _CRT_DOUBLE * value, char * str);      _CRT_DOUBLE dblval;      char str1[256] = "3.141592654";      Int retval = _atodbl(&dblval, str1);    ●int、long、double转化为CString    将数字转换为CString变量,可以使用CString的Format函数    %d %i→ int (long?)    %e %f→ double
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值