Thread: Conversion from hex to IEEE-754 float

  1. #1
    Unregistered
    Guest

    Unhappy Conversion from hex to IEEE-754 float

    I want to convert hexidecimal bytes from a serial device protocol to floating point

    for example: 0x40 0xb0 0x00 0x00 represents 5.5

    char msg[4];
    float *f;

    msg[0]=0x40;
    msg[1]=0xB0;
    msg[2]=0x00;
    msg[3]=0x00;

    f=(float*)msg doesn't seem to work

    does anyone know how to do this?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The reason it doesn't simply work good that way, is because floats are not stored in memory the same way that regular variables are. They have a skewed storage method. If they didn't, you could simply do:
    Code:
    union u {
        unsigned char c[4];
        float f;
    };
    You can try it, but I doubt you'll get the results you want.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >msg[0]=0x40;
    >msg[1]=0xB0;
    >msg[2]=0x00;
    >msg[3]=0x00;

    For a windows machine, should be:
    msg[3]=0x40;
    msg[2]=0xB0;
    msg[1]=0x00;
    msg[0]=0x00;

  4. #4
    Unregistered
    Guest
    Originally posted by quzah
    The reason it doesn't simply work good that way, is because floats are not stored in memory the same way that regular variables are. They have a skewed storage method. If they didn't, you could simply do:
    Code:
    union u {
        unsigned char c[4];
        float f;
    };
    You can try it, but I doubt you'll get the results you want.

    Quzah.
    ****
    Quzah, with the bytes in reverse order your solution works!!
    Thank you!.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-13-2009, 03:25 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 11
    Last Post: 03-24-2006, 11:26 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