十六进制转为float,float转为二进制

2016-12-06 19:09:43来源:cnblogs.com作者:c_sun_boke人点击

１、头文件：

` 1 /* 2  * mmath.h 3  * 4  *  Created on: Dec 6, 2016 5  *      Author: cow 6  */ 7  8 #ifndef MMATH_H_ 9 #define MMATH_H_10 #include <stdio.h>11 #include <stdlib.h>12 #include <tgmath.h>13 #include <string.h>14 15 long FloatTohex(float HEX);//浮点数到十六进制转换16 17 float BinarytoInt(char *ch,int num);//二进制到整数18 19 float BinarytoSmallNumber(char *ch,int num);//小数的二进制到小数20 21 float HextoFloat(char* ch); //传入８位１６进制　42F0E66622 23 24 #endif /* MMATH_H_ */`

２、source

`/* * mymath.c * *  Created on: Dec 6, 2016 *      Author: cow */#include "mymath.h"long FloatTohex(float HEX)//浮点数到十六进制转换1{ return *( long *)&HEX;}float BinarytoInt(char *ch,int num){    int retint = 0,i = 0;    for(i = 0;i< num;i++)    {        retint = retint + (ch[i] == '1' ? 1:0) * pow(2,(num-1-i));    }    return (float)retint;}float BinarytoSmallNumber(char *ch,int num){    float retf = 0.0;    int i = 0;    for(i = 0;i<num;i++)    {        retf = retf +  (ch[i] == '1'?1:0) * pow(2,(-(i + 1)) ) ;    }    return retf;}float HextoFloat(char* ch) //传入８位１６进制　42F0E666{    float returnData = 0.0;    int count = 8,i = 0;    char binary[32] = {0};    char  tmp = '0';    for(i = 0;i<count;i++)    {        tmp = ch[i];        switch(tmp)        {        case '0':            sprintf(binary+i*4,"%s","0000");            break;        case '/0':            sprintf(binary+i*4,"%s","0000");            break;        case '1':            sprintf(binary+i*4,"%s","0001");            break;        case '2':            sprintf(binary+i*4,"%s","0010");            break;        case '3':            sprintf(binary+i*4,"%s","0011");            break;        case '4':            sprintf(binary+i*4,"%s","0100");            break;        case '5':            sprintf(binary+i*4,"%s","0101");            break;        case '6':            sprintf(binary+i*4,"%s","0110");            break;        case '7':            sprintf(binary+i*4,"%s","0111");            break;        case '8':            sprintf(binary+i*4,"%s","1000");            break;        case '9':            sprintf(binary+i*4,"%s","1001");            break;        case 'A':            sprintf(binary+i*4,"%s","1010");            break;        case 'B':            sprintf(binary+i*4,"%s","1011");            break;        case 'C':            sprintf(binary+i*4,"%s","1100");            break;        case 'D':            sprintf(binary+i*4,"%s","1101");            break;        case 'E':            sprintf(binary+i*4,"%s","1110");            break;        case 'F':            sprintf(binary+i*4,"%s","1111");            break;        case 'a':            sprintf(binary+i*4,"%s","1010");            break;        case 'b':            sprintf(binary+i*4,"%s","1011");            break;        case 'c':            sprintf(binary+i*4,"%s","1100");            break;        case 'd':            sprintf(binary+i*4,"%s","1101");            break;        case 'e':            sprintf(binary+i*4,"%s","1110");            break;        case 'f':            sprintf(binary+i*4,"%s","1111");            break;        default :            printf("default /n");            break;        }    }    printf("bin = %s/n",binary);    /*     * 符号位（１）　　指数位（８）　　有效数字（２３）     * */    char symbol = binary[0];    char index[9] = {0};    char effectiveNumber[23] = {0};    memcpy(index,binary+1,8);    memcpy(effectiveNumber,binary+9,23);//    printf("symbol = %c/n",symbol);//    printf("index = %s/n",index);//    printf("effectiveNumber = %s/n",effectiveNumber);    float indexnum = 0.0,smallNumVal = 0.0,decnum = 0.0;    indexnum = BinarytoInt(index,8); //指数位大小    int numofmove = indexnum - 127;   //小数点移动值，可能为负数    if(numofmove > 0)   //小数点左移组成二进制    {        printf("(numofmove) = %d/n",(numofmove));        char * dec = (char *)malloc(( (numofmove) + 2) * sizeof(char));        memset(dec,0,((numofmove) + 2));        sprintf(dec,"%c",'1');        memcpy(dec + 1,effectiveNumber,(numofmove));        printf("dec = %s/n",dec);        decnum = BinarytoInt(dec,(numofmove) + 1);        printf("decnum = %f/n",decnum);        /*　//有效数位，去掉整数位才是小数位 */        char* smallNum = (char *)malloc(sizeof(char) * ((23 - (numofmove)) +1) );        memset(smallNum,0,((23 - (numofmove)) + 1));//最后一位/0        //memcpy(smallNum,effectiveNumber + numofmove,(23 - numofmove)); 遇到０停止拷贝        sprintf(smallNum,"%s",effectiveNumber + (numofmove));        printf("smallNum = %s/n",smallNum);        smallNumVal  = BinarytoSmallNumber(smallNum,(23 - numofmove));    }    //整数位是１    else if ( 0 ==numofmove )    {        smallNumVal  = BinarytoSmallNumber(effectiveNumber,(23 - numofmove));        decnum = 1;    }    //小数点右移组成二进制，此时要左移小数点，只有小数部分    else    {        //char* smallNum = (char *)malloc(sizeof(char) * ((23 ＋　(numofmove)) +1) );        char* smallNum = (char*)malloc(sizeof(char) * ( 23 - numofmove +1 ));        memset(smallNum,0,((23 - (numofmove)) + 1));        sprintf( (smallNum - numofmove - 1),"%s","1");        //smallNUm[(fabs(numofmove) - 1)] = '1';        sprintf((smallNum - numofmove ),"%s",effectiveNumber);        printf("smallNum = %s/n",smallNum);        smallNumVal  = BinarytoSmallNumber(smallNum,(23 - numofmove));        decnum = 0;    }    returnData = decnum + smallNumVal;　　//free 这里就不写了，懒    if(symbol == '0')  //符号位是０　表示正数    {        return returnData;    }    else    {        return -returnData;    }}`

3、主函数

`/* * main.c * *  Created on: Dec 2, 2016 *      Author: cow */#include "mymath.h"int main(){//    float f = HextoFloat("42F0E666");//    float f1 = HextoFloat("40B43333");//    float f2 = HextoFloat("3EE66666");//    printf("f = %f/n",f);//    printf("f1 = %f/n",f1);//    printf("f2 = %f/n",f2);    //float flh = 120.45;    float flh = -120.45;    char hex[9] = {0};    sprintf(hex,"%x",FloatTohex(flh));    printf("hex = %s/n",hex);    float f = HextoFloat(hex);    printf("f120.45 = %f/n",f);    return 0;}`

４、执行结果