Thread: Converting hexadecimal to float

  1. #1
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318

    Converting hexadecimal to float

    I would like to convert hexadecimal integer, for example "ABE46D41" to a float ("ABE46D41" should be ~14.87).
    Is this kind of convertion possible?
    Because floats are stored like this in the memory...

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Where do you get this from? A string?
    Code:
    #include <sstream>
    #include <iomanip>
    
    std::string input = "ABE46D41";
    std::istringstream iss(input) >> hex >> d;
    Try something like that.
    On the other hand, you can also store numbers as hex:
    Code:
    double d = 0xABE46D41;
    Provided that is a real hex number of course.

  3. #3
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Thanks for help.
    Last edited by maxorator; 06-25-2006 at 11:10 AM.

  4. #4
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    But I get those numbers directly as integers (or DWORD-s)...

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Please post what relevant code you have.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    maxorator: It sounds like you are trying to convert the bytes at a memory location into a float value. There are a number of gotchas with this, but this seems like what you are trying to do.
    Code:
    #include <iostream>
    
    int main() 
    {
       unsigned long value = 0x416DE4ABUL;
       float result = *reinterpret_cast<float*>(&value);
       std::cout << "value = " << std::hex << value << ", result = " << result << "\n"; 
       
       unsigned char bytes[] = {0xAB,0xE4,0x6D,0x41};
       result = *reinterpret_cast<float*>(bytes);
       std::cout << "result = " << result << "\n";
       
       return 0; 
    }
    
    /* my output
    value = 416de4ab, result = 14.8683
    result = 14.8683
    */
    Last edited by Dave_Sinkula; 06-25-2006 at 08:57 PM. Reason: Twiddled code a couple times.
    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.*

  7. #7
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    I already found one from google, something like:
    Code:
    union {
        int i;
        float f;
    } totfloat;
    And I made a function to use with it:
    Code:
    float tofloat(int data){
        totfloat.i=data;
        return totfloat.f;
    }
    Thanks for the help

  8. #8
    Registered Luser risby's Avatar
    Join Date
    Jun 2006
    Posts
    72
    Quote Originally Posted by maxorator
    I already found one from google, something like:
    Code:
    union {
        int i;
        float f;
    } totfloat;
    And I made a function to use with it:
    Code:
    float tofloat(int data){
        totfloat.i=data;
        return totfloat.f;
    }
    Thanks for the help
    This does not convert an int into a float. The union is a way of viewing the same piece of memory in different ways but the content of that memory does not change.

    Your tofloat() function stores a bit pattern that represents an integral number of a certain value. That integer is most likely 32 bits long, i.e. four bytes, but could be 16 bits or 64 bits depending on your platform. It then returns that bit pattern pretending that it is a float. It isn't; floats are stored with three components, viz: sign, exponent and mantissa.

    Actually, the compiler will do this conversion for you; all you need do is assign the int value into the float.

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by risby
    This does not convert an int into a float. The union is a way of viewing the same piece of memory in different ways but the content of that memory does not change.
    That appears to be the point. And yes, there are gotchas with this.
    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.*

  10. #10
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    risby, how else would you convert ABE46D41 to something near 14.87?
    This IS what I wanted to do.
    Following your advice would've made up 2883874113.00f out of ABE46D41.

    And another question - How is float built up exactly? I want to know a way to convert it into float manually. I like knowing these things
    Last edited by maxorator; 06-27-2006 at 09:01 AM.

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    There are different implementations of floating point, but I'll assume you are interested in IEEE 754.
    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.*

  12. #12
    Registered Luser risby's Avatar
    Join Date
    Jun 2006
    Posts
    72
    Quote Originally Posted by maxorator
    risby, how else would you convert ABE46D41 to something near 14.87?
    This IS what I wanted to do.
    Following your advice would've made up 2883874113.00f out of ABE46D41.

    And another question - How is float built up exactly? I want to know a way to convert it into float manually. I like knowing these things
    I am very sorry max. I was sleeping when I read the thread!

    I realise now that you have binary floats stored on disk or summat. Can you not simply read them in as floats? Why do they exist in integers at the point you need them?

  13. #13
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    I never even thought of reading them in as floats... good idea

    Still, could anyone answer my other questions:
    How is float built up exactly? I want to know a way to convert it into float manually

  14. #14
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Did you follow the link I posted? And no, you probably don't want "to convert it into float manually".

    What's the "big picture" of what you are trying to do?
    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.*

  15. #15
    Registered Luser risby's Avatar
    Join Date
    Jun 2006
    Posts
    72
    Quote Originally Posted by maxorator
    I never even thought of reading them in as floats... good idea

    Still, could anyone answer my other questions:
    How is float built up exactly? I want to know a way to convert it into float manually
    That is an excellent link Dave. Max, I guess you may not have realised it was a link as it was so neatly formatted; raw it is http://en.wikipedia.org/wiki/IEEE_754

    As well as diagrams it even has a C function to convert 32 bits into a float. You'll have to be really careful that your own compiler conforms exactly to the standard which the wiki article describes if you use the function through.

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