IEEE 754 floats

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dale Huffman

    IEEE 754 floats

    Is there a simple way to convert an IEEE-754 floating point ascii
    string ( "0x40400000 " = 3.0, 32bit ) into a float variable, without
    writing a function to do the math. I have transferred this across a
    network from a device I have no contol over and it sends all data as a
    string. Everything I have tried just converts from hex to decimal and
    adds a decimal point and a zero.

    string.atof("0x 40400000") returns 1077936128.0

    In case I'm not explaining clearly, what I'm looking for could be
    coded in C as follows:

    int a = 0x40400000;
    float *ap = (float *)&a;

    float myFloat = *ap;

    Sorry if the C offeded anyone in the Py crowd but I'm new to Python
    and so far it rocks - I just don't have the basics down yet.
  • Peter Otten

    #2
    Re: IEEE 754 floats

    Dale Huffman wrote:
    [color=blue]
    > Is there a simple way to convert an IEEE-754 floating point ascii
    > string ( "0x40400000 " = 3.0, 32bit ) into a float variable, without
    > writing a function to do the math. I have transferred this across a
    > network from a device I have no contol over and it sends all data as a
    > string. Everything I have tried just converts from hex to decimal and
    > adds a decimal point and a zero.
    >
    > string.atof("0x 40400000") returns 1077936128.0
    >
    > In case I'm not explaining clearly, what I'm looking for could be
    > coded in C as follows:
    >
    > int a = 0x40400000;
    > float *ap = (float *)&a;
    >
    > float myFloat = *ap;
    >
    > Sorry if the C offeded anyone in the Py crowd but I'm new to Python
    > and so far it rocks - I just don't have the basics down yet.[/color]
    [color=blue][color=green][color=darkred]
    >>> struct.unpack(" f", struct.pack("l" , int("0x40400000 ", 16)))[0][/color][/color][/color]
    3.0

    There may be simpler ways, though.

    Peter

    Comment

    • Tom B.

      #3
      Re: IEEE 754 floats


      "Dale Huffman" <dale_huffman@s teris.com> wrote in message
      news:a2a325a7.0 409140504.2d365 62b@posting.goo gle.com...[color=blue]
      > Is there a simple way to convert an IEEE-754 floating point ascii
      > string ( "0x40400000 " = 3.0, 32bit ) into a float variable, without
      > writing a function to do the math. I have transferred this across a
      > network from a device I have no contol over and it sends all data as a
      > string. Everything I have tried just converts from hex to decimal and
      > adds a decimal point and a zero.
      >
      > string.atof("0x 40400000") returns 1077936128.0
      >
      > In case I'm not explaining clearly, what I'm looking for could be
      > coded in C as follows:
      >
      > int a = 0x40400000;
      > float *ap = (float *)&a;
      >
      > float myFloat = *ap;
      >
      > Sorry if the C offeded anyone in the Py crowd but I'm new to Python
      > and so far it rocks - I just don't have the basics down yet.[/color]

      Have a look at the struct module in the standard distribution.

      Tom


      Comment

      • Robert Kern

        #4
        Re: IEEE 754 floats

        Dale Huffman wrote:
        [color=blue]
        > Is there a simple way to convert an IEEE-754 floating point ascii
        > string ( "0x40400000 " = 3.0, 32bit ) into a float variable, without
        > writing a function to do the math. I have transferred this across a
        > network from a device I have no contol over and it sends all data as a
        > string. Everything I have tried just converts from hex to decimal and
        > adds a decimal point and a zero.
        >
        > string.atof("0x 40400000") returns 1077936128.0
        >
        > In case I'm not explaining clearly, what I'm looking for could be
        > coded in C as follows:
        >
        > int a = 0x40400000;
        > float *ap = (float *)&a;
        >
        > float myFloat = *ap;
        >
        > Sorry if the C offeded anyone in the Py crowd but I'm new to Python
        > and so far it rocks - I just don't have the basics down yet.[/color]

        Take a look at the struct module.

        E.g.

        In [6]: import struct

        In [7]: s = struct.pack('i' , 0x40400000)

        In [8]: s
        Out[8]: '@@\x00\x00'

        In [9]: struct.unpack(' f', s)
        Out[9]: (3.0,)

        You might have to worry about endian-related issues, of course, but the
        struct module allows you to handle the various cases.

        --
        Robert Kern
        rkern@ucsd.edu

        "In the fields of hell where the grass grows high
        Are the graves of dreams allowed to die."
        -- Richard Harter

        Comment

        • Dale Huffman

          #5
          Re: IEEE 754 floats

          Thanks everyone it works... but does this seem like a kludgy (sp?) way
          to do this, or have I just not gotten to PythonThink mode yet.

          Comment

          • Robert Kern

            #6
            Re: IEEE 754 floats

            Dale Huffman wrote:[color=blue]
            > Thanks everyone it works... but does this seem like a kludgy (sp?) way
            > to do this, or have I just not gotten to PythonThink mode yet.[/color]

            I would submit that the task of converting an int to a float like that
            is a fairly rare task. It is exceedingly more common to convert a
            sequence of bytes to and from ints/floats. So Python opts for the more
            general solution, which happens to also be the best solution for the
            most common case.

            Additionally, the fine details of memory layout for basic types like
            ints and floats *ought* to be hidden from the user. Of course, you can
            still get at them if you need to via the struct module.

            If you run into this situation a lot, it's easy enough to write a
            function that encapsulates the kludge.

            For getting into PythonThink mode, type "import this" at the interactive
            prompt.

            --
            Robert Kern
            rkern@ucsd.edu

            "In the fields of hell where the grass grows high
            Are the graves of dreams allowed to die."
            -- Richard Harter

            Comment

            Working...