Convert hexadecimal to float, float to binary, float binary

Source: Internet
Author: User
Tags binary to decimal

Convert hexadecimal to float, float to binary, float binary

Paste the Code directly. Welcome to the discussion. Please indicate the source for reprinting. Thank you.

1. header file:

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); // convert a floating point to a hexadecimal value of 16 17 float BinarytoInt (char * ch, int num ); // binary to integer 18 19 float BinarytoSmallNumber (char * ch, int num); // binary to decimal 20 21 float HextoFloat (char * ch ); // input 8-bit hexadecimal 42F0E66622 23 24 # endif/* MMATH_H _*/

 

2. source

/** Mymath. c *** Created on: Dec 6, 2016 * Author: cow */# include "mymath. h "long FloatTohex (float HEX) // converts a floating point to a hexadecimal value. 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) // input 8-bit hexadecimal 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 ); /** symbol bit (1) exponent bit (8) valid number (23) **/char symbol = binary [0]; char index [9] = {0 }; char inclutivenumber [23] = {0}; memcpy (index, binary +); memcpy (inclutivenumber, 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 ); // index bit size int numofmove = indexnum-127; // decimal point move value, which may be a negative number if (numofmove> 0) // the decimal point moves left to form a binary {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, interval tivenumber, (numofmove )); printf ("dec = % s \ n", dec); decnum = BinarytoInt (dec, (numofmove) + 1); printf ("decnum = % f \ n ", decnum);/* // valid digit. The rounded digit is the decimal place */char * smallNum = (char *) malloc (sizeof (char) * (23-(numofmove )) + 1); memset (smallNum, 0, (23-(numofmove) + 1); // The last bit \ 0 // memcpy (smallNum, required tivenumber + numofmove, (23-numofmove); stops copying sprintf (smallNum, "% s", required tivenumber + (numofmove) at 0 )); printf ("smallNum = % s \ n", smallNum); smallNumVal = BinarytoSmallNumber (smallNum, (23-numofmove ));} // The integer is 1 else if (0 = numofmove) {smallNumVal = BinarytoSmallNumber (interval tivenumber, (23-numofmove); decnum = 1;} // the decimal point shifts to the right to form a binary, at this point, the decimal point must be shifted to the left. Only the decimal part of 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", inclutivenumber); printf ("smallNum = % s \ n", smallNum ); smallNumVal = BinarytoSmallNumber (smallNum, (23-numofmove); decnum = 0;} returnData = decnum + smallNumVal;
// Free will not be written here. if (symbol = '0') // if the symbol bit is 0, it indicates a positive number {return returnData;} else {return-returnData ;}}

 

3. Main Function

/* * 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. Execution result

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.