Help need with converting Hex string to IEEE format float

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • i_vincent@hotmail.com

    Help need with converting Hex string to IEEE format float

    Hi all,

    Newbie Python programmer here, so please be patient. I have spent all
    day googling for an answer to my problem, but everything I try fails to
    work (or works from the Interpreter with a set value but not from my
    code with dynamic values).

    Okay, here is the general gist of the problem. I am using Python to
    parse an output file (from a MAK Logger but that is not really
    important). Now I have some data that is contained on a line in this
    file like:

    80 00 00 00

    Each of these numbers is a Hex byte making up a four byte (32 bit
    Big-Endian) IEEE float. I have read this data into Python using
    readlines and then line.split(). This gives me:

    ['80', '00', '00', '00']

    I am then reading these in as:

    wib[0] + wib[1] + wib[2] + wib[3] = 8000000 as a string

    Now this is the point where I get stuck, I have tried various ways of
    implementing the pack/unpack methods of the struct module but with no
    luck.

    One example I tried was:

    wibble = struct.unpack(" f", struct.pack("l" , long(conv_str, 16)))
    OverflowError: long int too large to convert to int

    If I follow the examples I have found on the net using a set value of
    0x80000000 in them, everything works fine from the interpreter line.

    Arrrggggghhhh.

    Everything has worked really easily up this point but I am now stuck
    completely, I would be grateful for any help people can offer.
    Regards

    Ian Vincent

  • Fredrik Lundh

    #2
    Re: Help need with converting Hex string to IEEE format float

    <i_vincent@hotm ail.com> wrote:
    [color=blue]
    > Newbie Python programmer here, so please be patient. I have spent all
    > day googling for an answer to my problem, but everything I try fails to
    > work (or works from the Interpreter with a set value but not from my
    > code with dynamic values).
    >
    > Okay, here is the general gist of the problem. I am using Python to
    > parse an output file (from a MAK Logger but that is not really
    > important). Now I have some data that is contained on a line in this
    > file like:
    >
    > 80 00 00 00
    >
    > Each of these numbers is a Hex byte making up a four byte (32 bit
    > Big-Endian) IEEE float. I have read this data into Python using
    > readlines and then line.split(). This gives me:
    >
    > ['80', '00', '00', '00'][/color]

    how about:

    # convert to byte string
    import struct
    s = "".join([chr(int(c, 16)) for c in x])
    v = struct.unpack(" !f", s)

    or

    # convert to byte string, via the array module
    import array, struct
    a = array.array("B" , [int(c, 16) for c in x])
    v = struct.unpack(" !f", )

    </F>



    Comment

    • Fredrik Lundh

      #3
      Re: Help need with converting Hex string to IEEE format float

      > # convert to byte string, via the array module[color=blue]
      > import array, struct
      > a = array.array("B" , [int(c, 16) for c in x])
      > v = struct.unpack(" !f", )[/color]

      eh? should be:

      # convert to byte string, via the array module
      import array, struct
      a = array.array("B" , [int(c, 16) for c in x])
      v = struct.unpack(" !f", a)

      </F>



      Comment

      • Max M

        #4
        Re: Help need with converting Hex string to IEEE format float

        i_vincent@hotma il.com wrote:
        [color=blue]
        > Each of these numbers is a Hex byte making up a four byte (32 bit
        > Big-Endian) IEEE float. I have read this data into Python using
        > readlines and then line.split(). This gives me:
        >
        > ['80', '00', '00', '00'][/color]


        Oh, programmers loves this kind stuff. You should get tons of answers.

        ##
        st = '80 00 00 00'

        import binascii
        import struct

        s = ''.join([binascii.a2b_he x(s) for s in st.split()])
        v = struct.unpack(" f", s)[0]
        print v
        ##

        regards Max M

        --

        hilsen/regards Max M, Denmark


        IT's Mad Science

        Comment

        • Fredrik Lundh

          #5
          Re: Help need with converting Hex string to IEEE format float

          Max M wrote:
          [color=blue]
          > Oh, programmers loves this kind stuff. You should get tons of answers.[/color]

          data = '80 00 00 00'

          import Image
          v = Image.fromstrin g("F", (1, 1), data, "hex", "F;32BF").getpi xel((0, 0))

          </F>



          Comment

          • Christos TZOTZIOY Georgiou

            #6
            Re: Help need with converting Hex string to IEEE format float

            On Tue, 14 Dec 2004 16:57:02 +0100, rumours say that "Fredrik Lundh"
            <fredrik@python ware.com> might have written:
            [color=blue]
            >how about:
            >
            > # convert to byte string
            > import struct
            > s = "".join([chr(int(c, 16)) for c in x])
            > v = struct.unpack(" !f", s)[/color]

            I think that the third line in the snippet above could also be:

            s = "".join(x).deco de("hex")

            I am not sure I remember in which version of Python the hex codec was
            added, but it is handy.
            --
            TZOTZIOY, I speak England very best.
            "Be strict when sending and tolerant when receiving." (from RFC1958)
            I really should keep that in mind when talking with people, actually...

            Comment

            • Peter Hansen

              #7
              Re: Help need with converting Hex string to IEEE format float

              Christos TZOTZIOY Georgiou wrote:[color=blue]
              > s = "".join(x).deco de("hex")
              >
              > I am not sure I remember in which version of Python the hex codec was
              > added, but it is handy.[/color]

              Of course, binascii could do this since 2.0 or so, but not
              having to import another module *is* nice:
              [color=blue][color=green][color=darkred]
              >>> 'ff12'.decode(' hex')[/color][/color][/color]
              '\xff\x12'
              [color=blue][color=green][color=darkred]
              >>> import binascii
              >>> binascii.unhexl ify('ff12')[/color][/color][/color]
              '\xff\x12'

              Thanks for pointing it out, Christos.

              Comment

              • Ian Vincent

                #8
                Re: Help need with converting Hex string to IEEE format float

                Max M <maxm@mxm.dk> wrote in news:41bf121e$0 $280
                $edfadb0f@dread 12.news.tele.dk :[color=blue]
                >
                > ##
                > st = '80 00 00 00'
                >
                > import binascii
                > import struct
                >
                > s = ''.join([binascii.a2b_he x(s) for s in st.split()])
                > v = struct.unpack(" f", s)[0]
                > print v
                > ##[/color]

                This one worked great for what I was trying to do.

                Thanks to everybody for your help.

                TTFN

                Ian

                Comment

                Working...