#include <iostream>
#include <sstream>
#include <algorithm>
#include <cmath>
typedef unsigned int uint;
long double BinStringToD (string s);//将2进制字符串转化为10进制。
void main(void)
{
string s1="1111111111111111111111111111111111111111111111111111111111111111";
//要进行转换的64位2进制串
cout<<s1.length()<<endl;
cout.precision(20);
cout<<BinStringToD(s1)<<endl;
}
long double BinStringToD (string s)
{
string sU="0", sL="0";
uint tem1=0,tem2=0;
char chr='0';
//截取串,低32位赋给sL,高32位赋给sU.
if(s.length()>32)
{
sU=s.substr(0,(s.length()-32));
sL=s.substr(s.length()-32,32);
}
else
{
sL=s;
sU="";
}
//分别将sL,sU转化成双无符号整形tem1,tem2;
for(istringstream sinL (sL);sinL.get(chr);)
{
tem1<<=1;
tem1+=(chr-48);
}
for(istringstream sinU (sU);sU.length() && sinU.get(chr);)
{
tem2<<=1;
tem2+=(chr-48);
}
//精度丢失!!!!!!!!
long double t1=static_cast<long double>(tem1);
long double t2=static_cast<long double>(tem2);
t1=t1*pow(2,32);
return t1+t2;
}
输出结果为18446744073709552000
正确结果为18446744073709551615
请高手指点!!!!
[此贴子已经被作者于2006-6-11 20:03:35编辑过]