convert int to float to hex

12,412

Solution 1

try this:

int i = 2;
float f = (float)i;
printf("%#08X", *( (int*) &f ));

[EDIT]

@Corey:

let's parse it from inside out:

&  f = address of f = say address 0x5ca1ab1e
(int*)  &f = interpret the address 0x5ca1ab1e as integer pointer
*  ((int*)&f) = get the integer at address 0x5ca1ab1e

the following is more concise, but it's hard to remember the C language's operator associativity and operator precedence(i prefer the extra clarity of some added parenthesis and whitespace provides):

printf("%#08X", *(int*)&f);

Solution 2

printf("%#08x", convert); // prints zeros

This line is not going to work because you are telling printf that you are passing in an int (by using the %x) but infact you are passing it in a float.

What is your intention with this line? To show the binary representation of the floating point number in hex? If so, you may want to try something like this:

printf("%lx\n", *(unsigned long *)(&convert));

What this line is doing is taking the address of convert (&convert) which is a pointer to a float and casting it into a pointer to an unsigned long (note: that the type you cast into here may be different depending on the size of float and long on your system). The last * is dereferencing the pointer to an unsigned long into an unsigned long which is passed to printf

Solution 3

Given an int x, converting to float, then printing out the bytes of that float in hex could be done something like this:

show_as_float(int x) { 
   float xx = x;

   //Edit: note that this really prints the value as a double.
   printf("%f\t", xx);

   unsigned char *ptr = (unsigned char *)&xx;

   for (i=0; i<sizeof(float); i++)
       printf("%2.2x", ptr[i]);
}

The standards (C++ and C99) give "special dispensation" for unsigned char, so it's safe to use them to view the bytes of any object. C89/90 didn't guarantee that, but it was reasonably portable nonetheless.

Share:
12,412
Steller
Author by

Steller

Updated on June 30, 2022

Comments

  • Steller
    Steller almost 2 years

    Using scanf, each number typed in, i would like my program to print out two lines: for example

    byte order: little-endian
    
    > 2
         2 0x00000002
      2.00 0x40000000
    
    > -2
        -2 0xFFFFFFFE
     -2.00 0xC0000000
    

    I can get it to print out the 2 in hex but i also need a float and of course i cant scanf as one when i need to also scan as an int

    If i cast as a float when i try to printf i get a zero. If i scan in as a float i get the correct output. I have tried to convert the int to a float but it still comes out as zero.

    here is my output so far

    Int - float - hex
    
    byte order: little-endian
    
    >2
    
             2  0x000002
          2.00  00000000
    

    it looks like i am converting to a float fine why wont it print as a hex? if i scan in as a float i get the correct hex representation like the first example. this should be something simple. i do need to scan in as a decimal keep in mind i am running this in cygwin

    here is what i have so far..

    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
    
    
    int HexNumber;
        float convert;
    printf("Int - float - hex\n");
    
    
    
    int a = 0x12345678;
    unsigned char *c = (unsigned char*)(&a);
    if (*c == 0x78)
    {
        printf("\nbyte order: little-endian\n");
    }
    else
    {
        printf("\nbyte order: big-endian\n");
    }
    
    printf("\n>");
    scanf("%d", &HexNumber);
    printf("\n%10d  ",HexNumber);
    printf("%#08x",HexNumber);
    
    
    
    
    
    convert =  (float)HexNumber; // converts but prints a zero
    
    printf("\n%10.2f  ", convert); 
    printf("%#08x", convert); // prints zeros
    
    
    return 0;
    }