zoukankan      html  css  js  c++  java
  • 十六进制转为float,float转为二进制

    直接贴代码吧,欢迎交流,转载请注明出处,谢谢。

    1、头文件:

     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); //传入8位16进制 42F0E666
    22 
    23 
    24 #endif /* MMATH_H_ */

    2、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) //传入8位16进制 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 '':
                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 
    ");
                break;
            }
        }
        printf("bin = %s
    ",binary);
        /*
         * 符号位(1)  指数位(8)  有效数字(23)
         * */
        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
    ",symbol);
    //    printf("index = %s
    ",index);
    //    printf("effectiveNumber = %s
    ",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
    ",(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
    ",dec);
            decnum = BinarytoInt(dec,(numofmove) + 1);
            printf("decnum = %f
    ",decnum);
            /* //有效数位,去掉整数位才是小数位 */
            char* smallNum = (char *)malloc(sizeof(char) * ((23 - (numofmove)) +1) );
            memset(smallNum,0,((23 - (numofmove)) + 1));//最后一位
            //memcpy(smallNum,effectiveNumber + numofmove,(23 - numofmove)); 遇到0停止拷贝
            sprintf(smallNum,"%s",effectiveNumber + (numofmove));
            printf("smallNum = %s
    ",smallNum);
            smallNumVal  = BinarytoSmallNumber(smallNum,(23 - numofmove));
        }
        //整数位是1
        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
    ",smallNum);
            smallNumVal  = BinarytoSmallNumber(smallNum,(23 - numofmove));
            decnum = 0;
        }
        returnData = decnum + smallNumVal;
      //free 这里就不写了,懒
    if(symbol == '0') //符号位是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
    ",f);
    //    printf("f1 = %f
    ",f1);
    //    printf("f2 = %f
    ",f2);
    
        //float flh = 120.45;
        float flh = -120.45;
        char hex[9] = {0};
        sprintf(hex,"%x",FloatTohex(flh));
        printf("hex = %s
    ",hex);
        float f = HextoFloat(hex);
        printf("f120.45 = %f
    ",f);
        return 0;
    }

    4、执行结果

  • 相关阅读:
    htpasswd命令
    GitHub访问速度慢的解决方法
    easyui datagrid 首次不加载做法
    Excel日常操作
    补偿接口中循环一直执行sql的问题
    rabbitMq无法消费发送的q的问题
    Unicode与中文转换工具类方法(转)
    idea 一些插件配置
    线程安全的集合类、CopyOnWrite机制介绍(转)
    java websocket学习
  • 原文地址:https://www.cnblogs.com/csun/p/6137379.html
Copyright © 2011-2022 走看看