本文将介绍一些常用数据类型的使用
我们先定义一些常见类型变量借以说明 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); A