Convert int to float to hex

Using scanf, each entered number, I would like my program to print two lines: for example

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

I can make it print 2 in hexadecimal, but I also need floating and of course I cannot scan f as one when I need to also scan as int

If I use float while trying to printf, I get zero. If I scan as a float I get the correct output. I tried converting int to float, but it still appears as zero.

here is my conclusion so far

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

it looks like i am converting to a float why doesn't it print as hex? if I scan as a float, I get the correct hexadecimal representation similar to the first example. it should be something simple. I need to scan as decimal to mean I run this in cygwin

that's what i still have.

 #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; } 
+6
c floating-point integer floating-point-conversion
source share
3 answers

try the following:

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

[EDIT]

@Corey:

let him analyze it from the inside:

 & 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 brief but difficult to remember the associativity of the C language operator and the operator priority (I prefer the additional clarity of some added brackets and spaces):

 printf("%#08X", *(int*)&f); 
+8
source share
 printf("%#08x", convert); // prints zeros 

This line will not work because you tell printf that you are passing an int (using %x ), but in fact you are passing it to a float .

What is your intention on this line? To show a binary representation of a floating point number in hexadecimal? If so, you can try something like this:

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

This line uses the address convert ( &convert ), which is a pointer to a float and translates it into a pointer to an unsigned long (note: the type that you enter here may be different depending on the size of the float and for a long time on your system). The last * dereferences a pointer to an unsigned long to an unsigned long, which is passed to printf

+4
source share

Given that int x converts to float, then printing the bytes of that float to hex can 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]); } 

Standards (C ++ and C99) give "special permission" for unsigned char , so it’s safe to use them to view bytes of any object. C89 / 90 did not guarantee this, but nonetheless it was portable.

+1
source share

All Articles