Thread: Converting Float to Hexadecimal

  1. #1
    Registered User denizengt's Avatar
    Join Date
    Sep 2003
    Posts
    19

    Converting Float to Hexadecimal

    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?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: Converting Float to Hexadecimal

    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.
    Hope is the first step on the road to disappointment.

  3. #3
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    Code:
    printf("%xl",(usigned long)long_number); //outputting
    scanf("%xl",&long_number); //inputting
    
    long blah = (unsigned long) 0x8f6bcdee879l; //initializing
    hope this helps a bit
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >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
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-13-2009, 03:25 PM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM