本文将介绍一些常用数据类型的使用。 我们先定义一些常见类型变量借以说明 int i = 100; long l = 2001; float f=300.2; dou ble d=12345.119; char u sername[]="仔仔"; char temp[200]; char *bu f; CString str; _v ariant_t v 1; _bstr_t v 2; 一、其它数据类型转换为字符串 短整型(int) itoa(i,temp,10);///将i 转换为字符串放入 temp 中,最后一个数字表示十进制 itoa(i,temp,2); ///按二进制方式转换 长整型(long) ltoa(l,temp,10); 浮点数(float,dou ble) 用fcv t 可以完成转换,这是 MSDN 中的例子: int decimal, sign; char *bu ffer; dou ble sou rce = 3.1415926535; bu ffer = _fcv t( sou rce, 7, &decimal, &sign ); 运行结果:sou rce: 3.1415926535 bu ffer: '31415927' decimal: 1 sign: 0 decimal 表示小数点的位置,sign 表示符号:0 为正数,1 为负数 CString 变量 str = "2008 北京奥运"; bu f = (LPSTR)(LPCTSTR)str; BSTR 变量 BSTR bstrValu e = ::Sy sAllocString(L"程序员"); char * bu f = _com_u til::Conv ertBSTRToString(bstrValu e); Sy sFreeString(bstrValu e); Afx MessageBox (bu f); delete(bu f); CComBSTR 变量 CComBSTR bstrVar("test"); char *bu f = _com_u til::Conv ertBSTRToString(bstrVar.m_str); Afx MessageBox (bu f); delete(bu f); _bstr_t 变量 _bstr_t 类型是对BSTR 的封装,因为已经重载了=操作符,所以很容易使用 _bstr_t bstrVar("test"); const char *bu f = bstrVar;///不要修改bu f 中的内容 Afx MessageBox (bu f); 通用方法(针对非COM 数据类型) 用sprintf 完成转换 · char bu ffer[200]; · char c = '1'; · int i = 35; · long j = 1000; · float f = 1.7320534f; · sprintf( bu ffer, "%c",c); · sprintf( bu ffer, "%d",i); · sprintf( bu ffer, "%d",j); · sprintf( bu ffer, "%f",f); 二、字符串转换为其它数据类型 strcpy (temp,"123"); 短整型(int) i = atoi(temp); 长整型(long) l = atol(temp); 浮点(dou ble) d = atof(temp); CString 变量 CString name = temp; BSTR 变量 BSTR bstrValu e = ::Sy sAllocString...