程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 十六進制轉為float,float轉為二進制,float二進制

十六進制轉為float,float轉為二進制,float二進制

編輯:關於C語言

十六進制轉為float,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 '\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);
    /*
     * 符號位(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\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)); 遇到0停止拷貝
        sprintf(smallNum,"%s",effectiveNumber + (numofmove));
        printf("smallNum = %s\n",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\n",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\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;
}

4、執行結果

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved