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; }
c floating-point integer floating-point-conversion
Steller
source share