当前位置:文档之家› VC++常用数据类型及其操作详解

VC++常用数据类型及其操作详解

VC++常用数据类型及其操作详解―――――――――――――――――――――目录―――――――――――――――――――――一、VC常用数据类型列表二、常用数据类型转化2.1数学类型变量与字符串相互转换2.2 CString及string,char *与其他数据类型的转换和操作●CString,string,char*的综合比较●数学类型与CString相互转化●CString与char*相互转换举例●CString 与 BSTR 型转换●VARIANT 型转化成 CString 型2.3 BSTR、_bstr_t与CComBSTR2.4 VARIANT 、_variant_t 与 COleVariant附录CString及字符串转及操作详解参考书籍:CSDN,<<MFC深入浅出(Second Edit)>> ――――――――――――――――――――――――――――――――――――――――――――一、VC常用数据类型列表说明:(1)-------表示省略(2)1Byte=8Bit,字与机器有关,在8位系统中:字=1字节,16位系统中,1字=2字节,32位中:1字=4字节,64位中1字=8字节.不要搞混这些概念.二.常用数据类型转化及操作2.1 数学类型变量与字符串相互转换(这些函数都在STDLIB.H里)(1)将数学类型转换为字符串可以用以下一些函数:举例: _CRTIMP char * __cdecl _itoa(int, char *, int);//这是一个将数字转换为一个字符串类型的函数,最后一个int表示转换的进制如以下程序:int iTyep=3;char *szChar;itoa(iType,szChar,2);cout<<szChar;//输出为1010类似函数列表:_CRTIMP char * __cdecl _itoa(int, char *, int);//为了完整性,也列在其中_CRTIMP char * __cdecl _ultoa(unsigned long, char *, int);_CRTIMP char * __cdecl _ltoa(long, char *, int);_CRTIMP char * __cdecl _i64toa(__int64, char *, int);_CRTIMP char * __cdecl _ui64toa(unsigned __int64, char *, int);_CRTIMP wchar_t * __cdecl _i64tow(__int64, wchar_t *, int);_CRTIMP wchar_t * __cdecl _ui64tow(unsigned __int64, wchar_t *, int);_CRTIMP wchar_t * __cdecl _itow (int, wchar_t *, int);//转换为长字符串类型_CRTIMP wchar_t * __cdecl _ltow (long, wchar_t *, int);_CRTIMP wchar_t * __cdecl _ultow (unsigned long, wchar_t *, int);还有很多,请自行研究(2)将字符串类型转换为数学类型变量可以用以下一些函数:举例: _CRTIMP int __cdecl atoi(const char *);//参数一看就很明了char *szChar=”88”;int temp(0);temp=atoi(szChar);cout<<temp;类似的函数列表:_CRTIMP int __cdecl atoi(const char *);_CRTIMP double __cdecl atof(const char *);_CRTIMP long __cdecl atol(const char *);_CRTIMP long double __cdecl _atold(const char *);_CRTIMP __int64 __cdecl _atoi64(const char *);_CRTIMP double __cdecl strtod(const char *, char **);//_CRTIMP long __cdecl strtol(const char *, char **, int);//_CRTIMP long double __cdecl _strtold(const char *, char **);_CRTIMP unsigned long __cdecl strtoul(const char *, char **, int);_CRTIMP double __cdecl wcstod(const wchar_t *, wchar_t **);//长字符串类型转换为数学类型_CRTIMP long __cdecl wcstol(const wchar_t *, wchar_t **, int);_CRTIMP unsigned long __cdecl wcstoul(const wchar_t *, wchar_t **, int);_CRTIMP int __cdecl _wtoi(const wchar_t *);_CRTIMP long __cdecl _wtol(const wchar_t *);_CRTIMP __int64 __cdecl _wtoi64(const wchar_t *);还有很多,请自行研究2.2.CString及string,char *与其他数据类型的转换和操作(1)CString,string,char*的综合比较(这部分CSDN上的作者joise的文章<< CString,string,char*的综合比较>>写的很详细,请大家在仔细阅读他的文章.地址: /joise/或参考附录:(2)转换:●数学类型与CString相互转化数学类型转化为CString可用Format函数,举例:CString s;int i = 64;s.Format("%d", i)CString转换为数学类型:举例CString strValue("1.234");double dblValue;dblValue = atof((LPCTSTR)strValue);●CString与char*相互转换举例CString strValue(“Hello”);char *szValue;szValue=strValue.GetBuffer(szValue);也可用(LPSTR)(LPCTSTR)对CString// 进行强制转换.szValue=(LPSTR)(LPCTSTR)strValue;反过来可直接赋值:char *szChar=NULL;CString strValue;szChar=new char[10];memset(szChar,0,10);strcpy(szChar,”Hello”);strValue=szChar;●CString 与 BSTR 型转换CString 型转化成 BSTR 型当我们使用 ActiveX 控件编程时,经常需要用到将某个值表示成 BSTR 类型.BSTR 是一种记数字符串,Intel平台上的宽字符串(Unicode),并且可以包含嵌入的 NULL 字符。

可以调用 CString 对象的 AllocSysString 方法将 CString 转化成 BSTR:CString str;str = .....; // whateverBSTR bStr = str.AllocSysString();BSTR型转换为CString如果你在 UNICODE 模式下编译代码,你可以简单地写成:CString convert(BSTR bStr){if(bStr == NULL)return CString(_T(""));CString s(bStr); // in UNICODE modereturn s;}如果是 ANSI 模式CString convert(BSTR b){CString s;if(b == NULL)return s; // empty for NULL BSTR#ifdef UNICODEs = b;#elseLPSTR p = s.GetBuffer(SysStringLen(b) + 1);::WideCharToMultiByte(CP_ACP, // ANSI Code Page0, // no flagsb, // source widechar string-1, // assume NUL-terminatedp, // target bufferSysStringLen(b)+1, // target buffer lengthNULL, // use system default charNULL); // don''t care if default useds.ReleaseBuffer();#endifreturn s;}●VARIANT 型转化成 CString 型VARIANT 类型经常用来给 COM 对象传递参数,或者接收从 COM 对象返回的值。

你也能自己编写返回 VARIANT 类型的方法,函数返回什么类型依赖可能(并且常常)方法的输入参数(比如,在自动化操作中,依赖与你调用哪个方法。

IDispatch::Invoke 可能返回(通过其一个参数)一个包含有BYTE、WORD、float、double、date、BSTR 等等 VARIANT 类型的结果,(详见 MSDN 上的 VARIANT 结构的定义)。

在下面的例子中,假设类型是一个BSTR的变体,也就是说在串中的值是通过 bsrtVal 来引用,其优点是在 ANSI 应用中,有一个构造函数会把 LPCWCHAR 引用的值转换为一个 CString(见BSTR-to-CString 部分)。

在 Unicode 模式中,将成为标准的 CString 构造函数,参见对缺省::WideCharToMultiByte 转换的告诫,以及你觉得是否可以接受(大多数情况下,你会满意的)。

VARIANT vaData;vaData = m_com.YourMethodHere();ASSERT(vaData.vt == VT_BSTR);CString strData(vaData.bstrVal);你还可以根据 vt 域的不同来建立更通用的转换例程。

相关主题