威望0
积分7946
贡献0
在线时间763 小时
UID1
注册时间2021-4-14
最后登录2024-11-21
管理员
- UID
- 1
- 威望
- 0
- 积分
- 7946
- 贡献
- 0
- 注册时间
- 2021-4-14
- 最后登录
- 2024-11-21
- 在线时间
- 763 小时
|
功能:将一个浮点数字符串转换为IEEE754 64位二进制数(双精度)
解决的问题:8位单片机的C编译器无法实现双精度浮点数。
[mw_shl_code=c,true]//功能:将一个浮点数字符串转换为IEEE754 64位二进制数(双精度)
//解决的问题:8位单片机的C编译器无法实现双精度浮点数。
#include<stdlib.h>
//#include<stdio.h>
#include<string.h>
int IntToStr2(int num, char *ints2);
int FraToStr2(int num, int fraL10,char *fras2, int fraL2);
void ExpToStr2(int num, char *ints2);
void Str2ToStr16(char *str2, char *str16);
void Str10ToStr16(char *str,char *str16);
void main()
{
char str[100],str16[17];
puts("Input the number:"); gets(str);
Str10ToStr16(str,str16);
puts("IEEE754 64bit in hexadecimal:"); puts(str16);
}
void Str10ToStr16(char *str10,char *str16)
{ //high's code in www.dayi.net
int i,num,fra,intL2,fraL10,fraL2,k=-1,m=10,flag=0,zeros=0,extra;
char exp[12],str2[65],ints2[53],fras2[53],fras10[100];
for(i=0;i<11;i++)
exp = '0';
num = atoi(str10); //integer part
for(i=0;i<=strlen(str10);i++)
{
if(str10=='.')
{
k = i;
break;
}
}
if(k>-1)
{
for(i=k+1;i<=strlen(str10);i++)
{
fras10[i-k-1] = str10;
}
fras10[strlen(str10)-k] = '\0';
fra = atoi(fras10); //fraction part
}
else fra = 0;
str2[0] = '0';
if(num>0) //There is an integer part
{
intL2 = IntToStr2(num,ints2);
fraL10 = strlen(fras10);
fraL2 = 53 - intL2;
extra = FraToStr2(fra,fraL10,fras2,fraL2); //extra is the extra bit for rounding
for(i=0;i<intL2-1;i++)
str2[i+12] = ints2[intL2-2-i];
for(i=0;i<fraL2;i++)
str2[i+intL2+11] = fras2;
str2[64] = '\0';
}
else if(num==0) //There is no integer part
{
for(i=0;i<strlen(fras10)-1;i++)
m *= 10;
i = 0;
while(i<53) //52=64-12
{
fra *= 2;
if(flag==0)
{
zeros++;
if(fra/m==1)
{
flag = 1; //indicates first non zero
fra -= m;
}
}
else
{
str2[i+12] = fra/m+48;
if(fra/m==1)fra -= m;
i++;
}
}
extra = str2[64]; //extra bit for rounding
str2[64] = '\0';
ExpToStr2(1023-zeros,exp);
for(i=0;i<11;i++)
str2[11-i] = exp;
}
else
puts("num<0, something wrong!");
if(extra=='1')
{
i = 64;
while(i>=12)
{
i--;
if(str2=='1')
str2 = '0';
else
{
str2 = '1';
break;
}
}
if(i==12 && str2[12]=='0')
intL2++;
}
ExpToStr2(1022+intL2,exp); //1022=1023-1
for(i=0;i<11;i++)
str2[11-i] = exp;
puts("IEEE754 64bit in binary:");
puts(str2);
Str2ToStr16(str2,str16);
}
void Str2ToStr16(char *str2, char *str16)
{
int i,j,num;
for(i=0;i<16;i++)
{
num = 0;
for(j=0;j<4;j++)
{
num += (str2[i*4+3-j]-'0')<<j;
}
num += 48;
if(num>57) num += 7;
str16 = num;
}
str16[16] = '\0';
}
int IntToStr2(int num, char *ints2)
{
int i=0;
while(num>0)
{
if(num & 1) ints2 = '1';
else ints2 = '0';
num = num>>1;
i++;
}
ints2 = '\0';
return i;
}
void ExpToStr2(int num, char *ints2)
{
int i = 0;
while(num>0)
{
if(num & 1) ints2 = '1';
else ints2 = '0';
num = num>>1;
i++;
}
ints2[11] = '\0';
}
int FraToStr2(int num, int fraL10,char *fras2, int fraL2)
{
int i,m=10;
if(num==0)
{
for(i=0;i<fraL2;i++)
fras2 = '0';
}
else
{
for(i=0;i<fraL10-1;i++)
m *= 10;
for(i=0;i<=fraL2;i++)
{
num *= 2;
if(num/m==1)
{
fras2 = '1';
num -= m;
}
else fras2 = '0';
}
}
return fras2[fraL2]; //extra bit for rounding
}
[/mw_shl_code] |
|