配色: 字号:
C++_数据类型转换技巧
2012-08-06 | 阅:  转:  |  分享 
  
类型转换是将一种类型的值映射为另一种类型的值。进行数据类型的转换。

是在实际代码编写中经常遇到的问题,特别是字符串和其他类型的转换。

1.将字符串转换为整数

(1).转换函数

//双精度函数

doubleatof(

constcharstring

);

double_wtof(

constwchar_tstring

);

自适应

TCHAR:_tstof、_ttof

VS2005:_atof_l、_wtof_l、_atodbl、_atodbl_l



//整型函数

intatoi(

constcharstring

);

_int64_atoi64(

constcharstring

);

int_wtoi(

constwchar_tstring

);

_int64_wtoi64(

constcharstring

);

自适应

TCHAR:_tstoi、_ttoi、_tstoi64、_ttoi64

VS2005:_atoi_l、_wtoi_l、_atoi64_l、_wtoi64_l

//长整形函数

longatol(

constcharstring

);

long_wtol(

constwchar_tstring

);

自适应

TCHAR:_tstoi、_ttoi

VS2005:_atoi_l、_wtoi_l

可参考:http://blog.sina.com.cn/s/blog_4135af570100b0d9.html

(2).代码参考

Cstringting="1234";

intnum=atoi(ting);

num的值为1234.

CStringting="1234";

doublenum=atof(ting);

最终结果:num值为1234.0000000000000



2.int转换为字符串

cahr_itoa(

intvalue,

charstring,

intradix

);

char_i64toa(

_int64value,

charstring,

intradix

);

char_ui64toa(

unsigned_int64value,

charstring,

intradix

);

wchar_t_itow(

intvalue,

wchar_tstring,

intradix

);

wchar_t_i64tow(

_int64value,

wchar_tstring,

intradix

);

wchar_t_ui64tow(

unsigned_int64value,

wchar_tstring,

intradix

);

参数的意义:value是指要转换的整数,sring是用来存放转换后结果的便利,

radix是用来说明转换成几进制的数据,默认值是十进制数的。

转换的进制范围是二进制到三十六进制。示例代码:

intiii=123456789;

charii[100];

itoa(iii,ii,10);

ii中的结果就是字符串"123456789"

intiii=12;

charii[100];

itoa(iii,ii,2);

ii中的结果是字符串"1100"。



3.long转换为字符串



char_ltoa(longvalue,charstring,intradix);

wchar_t_ltow(longvalue,wchar_tstring,intradix);

其中,参数value为被转换的值,参数string为字符串缓冲区,radix为进制。

代码参考:

lingl=100;chartemp[10];

memset(temp,0,10);

ltoa(l,temp,10);



4.double转换为字符串



char_fcvt(doublevalue,intcount,intdec,intsign);

其中参数value为双精度数,参数count为转换的小数点后面的位数,dec表示小数点的位置,

sign表示符号。代码参数如下

intdecimal,sign;

charbuffer;

dobulesource=3.1415926535;

buffer=_fcbt(source,7,&decimal,&sign);

输出结果:sourec:3.1415926535;buffer:''31415927''decimal:1sign:0



5.日期类型转换为字符串



将一日期格式转换为字符串,利用了格式化函数,参考代码如下:

CTimeti=Ctime::GetCurrentTime();

CstringstrTemp("");

strTemp=ti.Format("%Y%m%d%H%H%S")



6.字符串转换为短整型



intatoi(constcharstring);

其中,参数constcharstring为要转换的字符串,返回值为转换的结果。



7.字符串转换为长整型



longatol(constcharstring)



8.字符串转换为双精度类型



doubleatof(constcharstring)

代码参考:

#include

#include



voidmain(void)

{

chars;dobulex;inti;longl;



s="-2309.12E-15";//测试atof

x=atof(s);

printf("atoftest:ASCIIstring:%s\tfloat:%e\n",s,x);



s="7.8912654773d210";//测试atof

x=atof(s);

printf("atoftest:ASCIIstring:%s\tfloat:%e\n",s,x);



s="-9885pigs";//测试atoi

i=atoi(s);

printf("atoitest:ASCIIstring:%s\t\tinteger:%d\n",s,i);



s="98854dollars";//测试atol

l=atol(s);

printf("atoitest:ASCIIstring:%s\t\tlong:%ld\n",s,l);

}

输出结果:

atoftest:ASCIIstring:-2309.12E-15float:-2.309120e-012

atoftest:ASCIIstring:7.8912654773d210float:7.891265E+210

atoftest:ASCIIstring:-9885pigsinteger:-9885

atoftest:ASCIIstring:98854dollarslong:98854



9.char和CString的相互转换



CString饱含了3个值:指向某个数据缓冲区的指针、该缓冲区中有的字符数

(是可存的,是位CString下的一个区)一个缓冲区长度。

有字符数的小可是0到该缓冲最长度值1的数(为字符串结有一个NULL字

符)。

(1)char转换为CString

☆值

☆利用格式化转换

CStringstrConvert;

TCHARP=_T("thisisachTostringtest");

strConvert=p;//值

strConvert.Format("%s",p);//格式化

(2)CString转换为char

☆制类型转换为LPCTSTR

CStringtheString((_T("Chartest"));

LPTSTRlpsz=(LPTSTR)(LPCTSTR)theString;

☆用strcpy

CStringtheString((_T("Chartest"));

LPTSTRlpsz=newTCHAR[theString.GetLength()+1];

_tcscopy(lpsz,theSting);

要说明的是,strcpy(可的_tcscpy)的二参数是constwcahr_t(Unicode)

constchar(ANSI),?编¢£将?自¥?其进行转换。

(3)用GetBuffer

如果要§currency1CString中的''“,有一个特?的??可用,fi就是GetBuffer,

的fl用是返回一个可写的缓冲指针。如果是–?§currency1字符?·短字符串,

例如:

CStrings(_T("Chartest"));

LPTSTRp=s.GetBuffer();

//?P的代码

s.ReleaseBuffer()//用?后??放。



10.CString”BSTR的相互转换



(1).CString转换为BSTR

?用CString?…的AllocSysString??将CString转化成BSTR,如:

CStringstr="thisisatest";

BSTRb=str.AllosysString();

str.FreeSystring();//最后要‰了?放

(2).BSTR转换为CString

CStringconvert(BSTRb)

{

CStrings;

if(b==NULL)

returns;

#ifdefUNICODE

s=b;

#else

LPSTRp=s.GetBuffer(SYsStringLen(b)+1);

::WidecharToMultiByte(CP_ACP,

0,

b,

-1,

p,

SysStringLen(b)+1,

NULL,

NULL);

s.ReleaseBuffer();

#endif

returns;

}



11.BSTR、_bstr_t”CComBSTR的



CComBSTR是ATL?BSTR的?,_bstr_t是C++?BSTR的?,

BSTR是32位指针,`′是指向字符串的缓冲区。

(1).char转换到BSTR

BSTRb=_com_util::ConvertStringToBSTR("数据")?

SysFreeString(bstrValue);

(2).BSTR转换到char

charp=_com_util::ConvertBSTRToString(b);

deletep;



12.typedef和typename?字

ˉ?个?字在˙用的?¨?常???,其实??ˇ,typedef是类型—义,

typename是类型?。

(1).typedef?字

类型说明经存在的数据类型一个别,—义字表示ˉ个类型的字。

类型说明的格式为typedef类型—义?

例如,用下面—义整型数的字:

typedefintINT_COUNT;//可(数£类型为整型)

INT_COUNTintcount;

intCount=123;

cout<
typedef可用来说明结、?a和类。如:

typedefstructbook{

floatfprice;

charszAuthor[20];

intnPage;}

BOOK;

viodfun()

{

BOOKmybook=newbook;

myBook->fPrice=40.80;

myBook->szAuthor="jf";

myBook->npage=200;

}

(2).typename?字

typename和class用在—义????,o的意义相,`tpyename是用来

编¢£中有一个表示类型。

Templatecalssfun{

Voidbar(){

T::Bletchp;

........

}

}

在代码中,T::Bletchp是指?,ˉ一点就???。`如果写成ˉ就?

出ˉ个问题:

Templateclassfun

{

Voidbar()

{

TypenameT::Bletchp;

}

}

在—义,?可用,template

templare?有相意义。



13.VARIANT的???



1.VARIANT的—义

VARIANT被为类型,其—义可参考头文件VC98\INCLUDE\OAIDL.H

结tagVARIANT—义。



2.引入VARIANT的原

目前?机言多种多,如C++、java、Basic、Pascal等,o各自又都有自己的

数据类型,VARIANT数据类型就?有跨言的特,?可表示(储存)意类型

的数据。C言的角度来讲,VARIANT其实是一个结,结中用一个(vt)表示,?

正真的数据则存储在union空中。



3.VARIANT的应用

利用VARIANT表示可一个整型数据:

VARIANTva;

inta=2001;

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

va.lVal=a;//值

利用VARIANT表示一个布尔值:

VARIANTva;

vb.vt=VT_BOOL;//指明整型数据

va.boolVal=VARIANT_TRUE;//值

利用VARIANT保存一个字符串:

VARIANTva;

va.vt=VT_BSTR;

va.bstrVal=SysAllocString(L"Hello,thisisatest");

面可看出ˉ种类型用来比较复杂,其实有?ˇ的??,采用VARIANT的?类_variant_t,

其值

可用制类型转换,其造函数?自¥??ˉ些数据类型。

代码参考如下:

longl=222;

ingi=100;

_varint_tlVal(l);

lVal=(long)i;

?可用ColeVariant来?化?VARIANT的操fl,代码参考如下:

COleVariantv3="thisisatest",v4=(long)1999?

CStringstr=(BSTR)v3.pbstrVal;

longi=v4.lVal;

献花(0)
+1
(本文系小云蔡首藏)