• 09-28-2003
denizengt
Converting a floating point number to hexadecimal
I'm trying to convert a floating point number (single precision). I want to show it's bit representation in hexadecimal, but I'm unsure on how to approach the problem. Any clues?

Hmm, I've somehow posted this in the C++ boards, how can I delete that thread?
• 09-28-2003
quzah
Quote:

Originally posted by denizengt
Converting a floating point number to hexadecimal
I'm trying to convert a floating point number (single precision). I want to show it's bit representation in hexadecimal, but I'm unsure on how to approach the problem. Any clues?

Hmm, I've somehow posted this in the C++ boards, how can I delete that thread?

Click on "Edit Post", check the delete box. Hit delete.

Back to your oringal issue: Don't. Why would you want to convert a floating point number to hex? There is no decimal in hex. Drop the decimal, make it integeral, and convert that way.

Or, multiply by ten, making it integral, conver to hex, and do the opposite when you convert back.

Quzah.
• 09-28-2003
Lynux-Penguin
Code:

```printf("%xl",(usigned long)long_number); //outputting scanf("%xl",&long_number); //inputting long blah = (unsigned long) 0x8f6bcdee879l; //initializing```
hope this helps a bit
• 09-28-2003
Dave_Sinkula
>I want to show it's bit representation in hexadecimal, but I'm unsure on how to approach the problem. Any clues?

Maybe something like this.
Code:

```#include <stdio.h> int main(void) {   union   {       float          f;       unsigned char  b[sizeof(float)];   } v = { 3.1415926535897932384626433832795F };   size_t i;   printf("%f is stored as ", v.f);   for ( i = 0; i < sizeof(v.b); ++i )   {       printf("%02X%c", v.b[i], i < sizeof(v.b) - 1 ? '-' : '\n');   }   return 0; } /* my output 3.141593 is stored as DB-0F-49-40 */```