浮点数转换成二进制
char *ToBinary(char *buffer,float x)
{
int k=0;
if(x<0)
{
buffer[k++]=\'-\';
x=-x;
}
int a=(int)x;
float b=x-a;
char temp[40];
int i=0;
if(a==0)
temp[i++]=\'0\';
while(a!=0)
{
temp[i++]=a%2+48;
a=a/2;
}
temp[i]=\'\0\';
i=i-1;
while(i>=0)buffer[k++]=temp[i--];
if(b==0)
{
buffer[k]=\'\0\';
return buffer;
}
buffer[k++]=\'.\';
for(i=0;i<23;i++)
{
int t=b*2;
buffer[k++]=t+48;
b=b*2-t;
}
if(b!=0)
cout<<"ERROR"<<endl;
buffer[k++]=\'\0\';
return buffer;
}