# Floating point to hexadecimal, arranged by the most "important" bits

• 12-02-2007
Slither
Floating point to hexadecimal, arranged by the most "important" bits
Hi, I have this assignment to do, in which the program asks the user for floating point input, after getting this the floating point is printed as a hexadecimal bit by bit, arranging so that the most important bit is first etc.

This is what I get, it seems to work quite properly on presenting the hexadecimal sometimes. Sometimes it just prints a whole lot of F's even though they really should not be there. Could someone please guide me to the righ direction?

Clearly I'm not arranging these yet, first I'd like it to present the data correctly.

Code:

```#include <stdio.h> int main (void) {         int i;         double nbr;         char *ptr;         printf("Type a floating point:\n");         scanf("%lf", &nbr);         ptr = &nbr;         printf("\Number %lf as a hexadecimal is \n", nbr);         for(i=0;i<sizeof(double);i++)                 printf("%X ", *ptr++);         return 0; }```
• 12-02-2007
MacGyver
If you're printing a number bit by bit, what is the point of doing it in hexadecimal? Do you mean byte by byte?

You're going to have a number of issues here. For one, floating point representation in binary is usually (like always?) not the same as regular integer binary representations, which means you're not going to get what you might expect. Example: If you enter in 15, and you expect to see F, you probably won't see it.

Another issue is endianess. If you enter the 2 byte hexadecimal value AABB, on some machines it will appear in memory as BBAA. Reading it sequentially byte by byte that way might cause this type of seemingly weird behavior.

Are you sure your assignment wants a floating point number? And what format do they actually want it printed back in? Just whatever the comes back in whatever format?
• 12-02-2007
Slither
The assignment was to "create a program which asks for double and prints it byte by byte as a hex. The bytes should be arranged so that the most important byte comes first." It is just a school assignment, to be more familiar with floating points I guess.

We did the same with integers excluding the arrangment of bytes at school, so I am sure that this needs to be double.
• 12-02-2007
MacGyver
Depending on your architecture, you may have to do some bit shifting then if you want the raw bytes in the exact order specified.

Otherwise, if he wants it broken down so that 15.15 comes out as F.F, then that's different.
• 12-02-2007
Slither
Quote:

Depending on your architecture, you may have to do some bit shifting then if you want the raw bytes in the exact order specified.

Otherwise, if he wants it broken down so that 15.15 comes out as F.F, then that's different.
The arrangement is not the biggest problem now, I'll see into that later on. The problem is how my code (as pasted above) represents the number as a hex right on some occasions. Either that or I don't understand the concept.

As I said, we did the same thing for integers while at school, so the teacher wants it as I explained.

The program should work like this one: http://babbage.cs.qc.edu/IEEE-754/Decimal.html

Edit: I found a solution, pointer should be unsigned char :)
• 12-02-2007
Salem
Well first you have to realise that floats/doubles are composed of 'sign', 'mantissa' and 'exponent' groups of bits.

Typically, you memcpy the float/double into say an unsigned long, then use bitwise ops to extract individual bits.
• 12-02-2007
ZuK
make char a pointer to unsigned char to print hex
Code:

```#include <stdio.h> int main (void) {         int i;         double nbr;         unsigned char *ptr;         printf("Type a floating point:\n");         scanf("&#37;lf", &nbr);         ptr = (unsigned char *)&nbr;         printf("\nNumber %lf as a hexadecimal is \n", nbr);         for(i=0;i<sizeof(double);i++)                 printf("%X ", *ptr++);         return 0; }```
Kurt
• 12-02-2007
Slither
I got it.. The final code (including the arrangement is as follows):

Code:

```#include <stdio.h> #include <stdlib.h> int main (void) {         int i;         double nbr;         unsigned char *ptr;         printf("Input a floating point:\n");         scanf("%lf", &nbr);         ptr = &nbr;         ptr = (ptr+7);         printf("\Number %lf as a hexadecimal is \n", nbr);         for(i=0;i<sizeof(double);i++){                 printf("%X ", *ptr--);         }         return 0; }```
It seems to work ok.