6

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;
}
3
  • Can you please show us some code? Commented Jan 20, 2010 at 16:41
  • Doesn't print zero for me when I enter 1 as input. What are you trying to achieve? What's your input number? Commented Jan 20, 2010 at 16:52
  • my input number is a 2 just like the input i want Commented Jan 20, 2010 at 16:54

3 Answers 3

8

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);
Sign up to request clarification or add additional context in comments.

4 Comments

it looks like I edited my answer to include the same thing at the same time.
if (sizeof(int) != sizeof(float)) you have problems.
can you explain what *((int *)&f)); thats actually doing. I understand pointers and addresses.
If you understand pointers and addresses, just work through it and you should see what its doing.
4
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

4 Comments

Thank you for your explaination of what you did. Very helpful
I am curious how i can get it to format like this: 0xFFFFFFFE instead of 0xffffffe. - if i use uppercase X its like 0XFFFFFFFE i need the x small and the rest big..
@Corey use "0x%08X" instead of "%#08X"
Trent, you have been very helpful.. I would have made yours the solution. However, you posted second. Unless you really want it. I gave you points though..
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.