您的位置:首页 > 其它

字符串,数字之间的转换

2008-08-05 11:43 232 查看
数字---->字符串

CString NtoS(double d)

{

int decimal, sign;

char *buffer;

buffer = _ecvt( d, 16, &decimal, &sign );

CString str=buffer;

if(decimal>=0 && decimal<=16) str.Insert(decimal,".");

else if(decimal>16)

{

for(int i=str.GetLength();i<decimal;i++) str+="0";

str+=".0";

}

else

{

for(int i=0;i<-decimal;i++) str.Insert(0,"0");

str.Insert(0,".");

}

if(sign==1) str.Insert(0,"-");

return str;

}

字符串---->数字

double StoN(CString str)

{

char *stopstring;

double x;

x = strtod( str.GetBuffer(0), &stopstring );

m_strTmp=stopstring;

return x;

}

10进制-----〉2进制

void CCalculation::Dec2Bin(CString *strExp)

{

bool bMinus=0;

if(strExp->GetAt(0)=='-')

{

bMinus=1;

strExp->Delete(0);

}

int pos=strExp->Find('.');

CString str,strDec;

_int64 nDecInt;

double dDec;

if(pos!=-1)

{

strDec=strExp->Left(pos);

if(strDec.Compare("4294967295")>0 && strDec.GetLength()>10 || strDec.GetLength()>10)

{

*strExp="太大无法转换";

return;

}

nDecInt=atoi(strDec.GetBuffer(0));

strDec=strExp->Right(strExp->GetLength()-pos);

}

else

{

if(strExp->Compare("4294967295")>0 && strExp->GetLength()>10 || strExp->GetLength()>10)

{

*strExp="太大无法转换";

return;

}

nDecInt=atoi(strExp->GetBuffer(0));

}

strExp->Empty();

while(nDecInt!=0)

{

_int64 nTmp=nDecInt%2;

str.Format("%d",nTmp);

nDecInt/=2;

strExp->Insert(0,str);

}

*strExp+=".";

if(pos!=-1)

{

dDec=StoN(strDec);

int nCount=0;

while(dDec!=0)

{

dDec*=2;

int nTmp=int(dDec);

str.Format("%d",nTmp);

*strExp+=str.Left(pos);

dDec-=nTmp;

if(++nCount==17) break;

}

}

if(bMinus) strExp->Insert(0,"-");

}

10进制---------〉16进制

void CCalculation::Dec2Hex(CString *strExp/*strExp须为数字*/)

{

bool bMinus=0;

if(strExp->GetAt(0)=='-')

{

bMinus=1;

strExp->Delete(0);

}

int pos=strExp->Find('.');

CString str,strDec;

int nDecInt;

double dDec;

if(pos!=-1)

{

strDec=strExp->Left(pos);

nDecInt=atoi(strDec.GetBuffer(0));

strDec=strExp->Right(strExp->GetLength()-pos);

}

else

{

nDecInt=atoi(strExp->GetBuffer(0));

}

strExp->Empty();

while(nDecInt!=0)

{

int nTmp=nDecInt%16;

if(nTmp==10) str="a";

else if(nTmp==11) str="b";

else if(nTmp==12) str="c";

else if(nTmp==13) str="d";

else if(nTmp==14) str="e";

else if(nTmp==15) str="f";

else str.Format("%d",nTmp);

nDecInt/=16;

strExp->Insert(0,str);

}

*strExp+=".";

if(pos!=-1)

{

dDec=StoN(strDec);

int nCount=0;

while(dDec!=0)

{

dDec*=16;

int nTmp=int(dDec);

if(nTmp==10) str="a";

else if(nTmp==11) str="b";

else if(nTmp==12) str="c";

else if(nTmp==13) str="d";

else if(nTmp==14) str="e";

else if(nTmp==15) str="f";

else str.Format("%d",nTmp);

*strExp+=str.Left(pos);

dDec-=nTmp;

if(++nCount==17) break;

}

}

if(bMinus) strExp->Insert(0,"-");

if(strExp->Find("-1")!=-1 && bMinus!=1) *strExp="太大无法表示";

}

void CCalculation::Bin2Dec(CString *strExp)

{

int len,i,index,pos=strExp->Find("xb");

CString strTmp,strDF;

char ch;

double dx;

while(pos!=-1)

{

dx=0;strTmp="";strDF="";

strExp->Delete(pos,2);

for(i=pos-1;i>=0;i--)

{

ch=strExp->GetAt(i);

if(ch>=50 && ch<=57 || ch>=97 && ch<=102)

{

*strExp="ERROR_二进制数越界_";

return;

}

if(ch=='0' || ch=='1' ||ch==46)

{

strTmp.Insert(0,strExp->Mid(i,1));

strExp->Delete(i);

}

else break;

}

if(i==pos-1) {*strExp="ERROR_缺少二元运算符_";return;}

index=i;

pos=strTmp.Find(".");

if(pos!=-1)

{

strDF=strTmp.Right(strTmp.GetLength()-pos-1);

strTmp.Delete(pos,strTmp.GetLength()-pos);

}

strTmp.MakeReverse();

len=strTmp.GetLength();

for(i=0;i<len;i++)

{

ch=strTmp.GetAt(i);

dx+=(ch-48)*pow(2,i);

}

len=strDF.GetLength();

for(i=0;i<len;i++)

{

ch=strDF.GetAt(i);

dx+=(ch-48)*pow(2,-i-1);

}

strTmp=NtoS(dx);

strExp->Insert(index+1,strTmp);

pos=strExp->Find("xb");

}

}

void CCalculation::Dec2Hex(CString *strExp/*strExp须为数字*/)

{

bool bMinus=0;

if(strExp->GetAt(0)=='-')

{

bMinus=1;

strExp->Delete(0);

}

int pos=strExp->Find('.');

CString str,strDec;

int nDecInt;

double dDec;

if(pos!=-1)

{

strDec=strExp->Left(pos);

nDecInt=atoi(strDec.GetBuffer(0));

strDec=strExp->Right(strExp->GetLength()-pos);

}

else

{

nDecInt=atoi(strExp->GetBuffer(0));

}

strExp->Empty();

while(nDecInt!=0)

{

int nTmp=nDecInt%16;

if(nTmp==10) str="a";

else if(nTmp==11) str="b";

else if(nTmp==12) str="c";

else if(nTmp==13) str="d";

else if(nTmp==14) str="e";

else if(nTmp==15) str="f";

else str.Format("%d",nTmp);

nDecInt/=16;

strExp->Insert(0,str);

}

*strExp+=".";

if(pos!=-1)

{

dDec=StoN(strDec);

int nCount=0;

while(dDec!=0)

{

dDec*=16;

int nTmp=int(dDec);

if(nTmp==10) str="a";

else if(nTmp==11) str="b";

else if(nTmp==12) str="c";

else if(nTmp==13) str="d";

else if(nTmp==14) str="e";

else if(nTmp==15) str="f";

else str.Format("%d",nTmp);

*strExp+=str.Left(pos);

dDec-=nTmp;

if(++nCount==17) break;

}

}

if(bMinus) strExp->Insert(0,"-");

if(strExp->Find("-1")!=-1 && bMinus!=1) *strExp="太大无法表示";

}

void CCalculation::Dec2Oct(CString *strExp)

{

bool bMinus=0;

if(strExp->GetAt(0)=='-')

{

bMinus=1;

strExp->Delete(0);

}

int pos=strExp->Find('.');

CString str,strDec;

int nDecInt;

double dDec;

if(pos!=-1)

{

strDec=strExp->Left(pos);

nDecInt=atoi(strDec.GetBuffer(0));

strDec=strExp->Right(strExp->GetLength()-pos);

}

else

{

nDecInt=atoi(strExp->GetBuffer(0));

}

strExp->Empty();

while(nDecInt!=0)

{

int nTmp=nDecInt%8;

str.Format("%d",nTmp);

nDecInt/=8;

strExp->Insert(0,str);

}

*strExp+=".";

if(pos!=-1)

{

dDec=StoN(strDec);

int nCount=0;

while(dDec!=0)

{

dDec*=8;

int nTmp=int(dDec);

str.Format("%d",nTmp);

*strExp+=str.Left(pos);

dDec-=nTmp;

if(++nCount==17) break;

}

}

if(bMinus) strExp->Insert(0,"-");

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: