Re: hexadecimal to float conversion

Discussion in 'C Programming' started by SM Ryan, Aug 20, 2005.

  1. SM Ryan

    SM Ryan Guest

    wrote:
    # I am doing embedded programming and coding using NIOS processor.
    # I am reading a 32 bit value from a register and storing it in a 32 bit
    # data type.
    # for e.g.
    #
    # alt_u32 gain = 0x42ED4000;

    If that is a floating point value encoded as hex string,
    you might be able to do
    union {
    int i;
    float f;
    } switcheroo;
    switcheroo.i = 0x42ED4000;
    floatvalue = switcheroo.f;
    or perhaps
    int i = 0x42ED4000;
    float f;
    memcpy(&i,&f,sizeof f);

    Depends on what exactly the hex string is encoding. There could also
    be byte flopping issues.

    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    Haven't you ever heard the customer is always right?
     
    SM Ryan, Aug 20, 2005
    #1
    1. Advertising

  2. SM Ryan

    Joe Wright Guest

    SM Ryan wrote:
    > wrote:
    > # I am doing embedded programming and coding using NIOS processor.
    > # I am reading a 32 bit value from a register and storing it in a 32 bit
    > # data type.
    > # for e.g.
    > #
    > # alt_u32 gain = 0x42ED4000;
    >
    > If that is a floating point value encoded as hex string,
    > you might be able to do
    > union {
    > int i;
    > float f;
    > } switcheroo;
    > switcheroo.i = 0x42ED4000;
    > floatvalue = switcheroo.f;
    > or perhaps
    > int i = 0x42ED4000;
    > float f;
    > memcpy(&i,&f,sizeof f);
    >
    > Depends on what exactly the hex string is encoding. There could also
    > be byte flopping issues.
    >

    This one..

    #include <stdio.h>
    int main(void) {
    union {
    int i;
    float f;
    } switcheroo;
    switcheroo.i = 0x42ED4000;
    printf("0x%X %f\n", switcheroo.i, switcheroo.f);
    return 0;
    }

    ...prints..
    0x42ED4000 118.625000
    --
    Joe Wright
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
     
    Joe Wright, Aug 21, 2005
    #2
    1. Advertising

  3. SM Ryan

    Guest

    Hi,
    Thanks a lot for your solutions. I was making some error in
    typecasting. It works fine now.
     
    , Aug 25, 2005
    #3
  4. writes:
    > Thanks a lot for your solutions. I was making some error in
    > typecasting. It works fine now.


    Solutions to what?

    Search this newsgroup for the phrase "Context, dammit!", and follow
    the advice you find.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Aug 25, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bd
    Replies:
    0
    Views:
    642
  2. hexadecimal to float conversion

    , Aug 19, 2005, in forum: C Programming
    Replies:
    4
    Views:
    1,731
    Keith Thompson
    Aug 20, 2005
  3. k3n3dy
    Replies:
    15
    Views:
    998
    dan2online
    Apr 20, 2006
  4. Carsten Fuchs
    Replies:
    45
    Views:
    1,578
    James Kanze
    Oct 8, 2009
  5. Simon
    Replies:
    3
    Views:
    495
    James Kuyper
    Jun 1, 2011
Loading...

Share This Page