郭庆帅 发表于 2017-6-21 18:07:12

新技能ieee754类型转化成float

IEEE 754 标准是IEEE二进位浮点数算术标准(IEEE Standard for Floating-Point Arithmetic)的标准编号 ,等同于国际标准ISO/IEC/IEEE 60559 。该标准由美国电气电子工程师学会(IEEE)计算机学会旗下的微处理器标准委员会(Microprocessor Standards Committee, MSC)发布。这个标准定义了表示浮点数的格式(包括负零-0)与反常值(denormal number),一些特殊数值(无穷(Inf)与非数值(NaN)),以及这些数值的「浮点数运算子」;它也指明了四种数值修约规则和五种例外状况(包括例外发生的时机与处理方式 ...废话太多 .................

iieee754类型的数据 一般是用在仪器的读.和精确数值的计算
如何计算呢这才是我们要计算的
一中比较简单的
long int x = 0x3f8ccccd;// 1.1
float b = *(float*)&x;
printf("b=%f\n",b); 解释:
0x3f8ccccd是浮点数1.1的IEEE754格式,以十六进制显示。&x表示取1.1的地址,得到一个int型指针。(float*)&x表示将&x这个int型指针强制转换成float型指针。*(float*)&x表示这个float型指针所指向的数,也就是1.1。最后b的值显示就是1.1


第二种办法


/*作者:左健   单位:cqccri   时间:2014.12.20*/


#include<iostream>
#include<math.h>
using namespace std;
int a,b,*pa=a,*pb=b;

//========================================================================================
//Description : 将十进制数据整数部分转换为二进制格式
//Argument(s) : none
//Return(s)   : none
//Caller(s)   :                                                          
//Note(s)   : none
//========================================================================================
void Decimal_Integer_To_Binary(int x)
{
        int m,i;
        for(i=0;;i++)
        {
                *(pa+i)=x%2;
                m=x/2;
                x=m;
                if(m==0)
                {
                        *(pa+32)=i;//此时的i比实际的位数个数少1
                        break;
                }
        }
        for(int j(0);j<=i;j++)
                *(pb+j)=*(pa+i-j);
}
//========================================================================================
//Description : 将接收到的四个字节的IEEE754格式数据转换浮点数
//Argument(s) : none
//Return(s)   : 浮点数
//Caller(s)   :                                                          
//Note(s)   : none
//========================================================================================
float IEEE754_To_Float(int* p4x8)
{
        int i=0,j=0,flag=0,Exp_Part=0,exp_part=0;
        int        ieee754_result={0},Decimal_Part={0};
        __int64 Integer_Part={0};
        float back_float=0.0,multi0_5=1.0;
        for (i=0;i<4;i++)
        {
                Decimal_Integer_To_Binary(*(p4x8+i));
                flag=a;
                for(j=0;j<=flag;j++)
                        ieee754_result=b;                                        //ieee754_result~ieee754_result
        }
        for (i=2;i<=9;i++)
        {
                Exp_Part+=ieee754_result*(1<<(9-i));
        }
        exp_part=Exp_Part-127;                                                                                        //取得指数及移位个数
        if (exp_part>=0)
        {
                Integer_Part=1;                                                                                        //1~32
                for (i=0;i<exp_part;i++)
                {
                        if (i>=23)break;                                                                                //ieee754_result[]维数不能超过32,Integer_Part[]就不能超过24
                        Integer_Part=ieee754_result;                                        //装入整数部分
                }
                        Integer_Part=exp_part+1;                                                        //整数部分的个数
                if(exp_part<23)
                {
                        for (i=0;i<23-exp_part;i++)
                        {
                                Decimal_Part=ieee754_result;        //1~32,装入小数部分
                        }
                        Decimal_Part=23-exp_part;                                                                //小数部分的个数
                }
        }
        else
        {
                exp_part=-exp_part;
                Integer_Part=0;                                                                                        //整数部分的个数
                for (i=0;i<exp_part;i++)
                {
                        Decimal_Part=0;                                                                        //1~32,装入小数部分.先全部填0,最后在exp_part处填1替换
                }
                Decimal_Part=1;
                for (i=0;i<23;i++)
                {
                        Decimal_Part=ieee754_result;                //1~32,装入小数部分
                }
                Decimal_Part=exp_part+23;                                                                //小数部分的个数               
        }
        for (i=1;i<=Integer_Part;i++)                                                                //获得整数部分
        {
                if(i>24) break;                                                                                                //Integer_Part不能超过24
                back_float+=Integer_Part*(1<<(Integer_Part-i));
        }
        for (i=1;i<=Decimal_Part;i++)                                                                //获得小数部分
        {
                if(i>24) break;
                for (j=1;j<=i;j++)
                {
                        multi0_5*=0.5;
                }
                back_float+=Decimal_Part*multi0_5;
                multi0_5=1.0;
        }
       
        if (1==ieee754_result)
        {
                back_float=-back_float;
        }
        for (i=0;i<33;i++)
        {
                *(pa+i)=0;
                *(pb+i)=0;
        }

       
        return back_float;
}
void main()
{
        int input_result={0};
        float result_float=0.0;
        while(1)
        {
                printf("请输入IEEE754格式的四个字节数(以英文逗号分隔,如:3f,80,00,00):\n");
                scanf("%x,%x,%x,%x",input_result,input_result+1,input_result+2,input_result+3);
                result_float=IEEE754_To_Float(input_result);
                printf("%x,%x,%x,%x的浮点数的结果为:%f\n",*(input_result+0),*(input_result+1),*(input_result+2),*(input_result+3),result_float);
//                printf("%x,%x,%x,%x的浮点数*408-401的结果为:%f\n",*(input_result+0),*(input_result+1),*(input_result+2),*(input_result+3),result_float*408-401);
        }
}

相信大家已经知道怎么可以转换了
页: [1]
查看完整版本: 新技能ieee754类型转化成float