how to convert 4 bytes into a float ?

Discussion in 'Python' started by Jean-Baptiste PERIN, Feb 8, 2005.

  1. I read 4 bytes from a binary file.

    These bytes represent a floating point number (mantisse exponent form)

    How can I get a float from these bytes ?
    Jean-Baptiste PERIN, Feb 8, 2005
    #1
    1. Advertising

  2. Jean-Baptiste PERIN <> wrote:

    > I read 4 bytes from a binary file.
    >
    > These bytes represent a floating point number (mantisse exponent form)
    >
    > How can I get a float from these bytes ?


    See the docs for module struct, specifically the struct.unpack function,
    if the float is in the binary format your machine expects (or possibly
    that but with the wrong endianity). If the float's binary format is
    different from what your machine uses, you're in for a lot of painful
    bit-twiddling.


    Alex
    Alex Martelli, Feb 8, 2005
    #2
    1. Advertising

  3. Alex Martelli a écrit :

    > Jean-Baptiste PERIN <> wrote:
    >
    >
    >>I read 4 bytes from a binary file.
    >>
    >>These bytes represent a floating point number (mantisse exponent form)
    >>
    >>How can I get a float from these bytes ?

    >
    >
    > See the docs for module struct, specifically the struct.unpack function,
    > if the float is in the binary format your machine expects (or possibly
    > that but with the wrong endianity). If the float's binary format is
    > different from what your machine uses, you're in for a lot of painful
    > bit-twiddling.
    >
    >
    > Alex


    I'll have to handle Intel-PC, DEC-VAX and MIPS-SUN/SGI numbers
    So I can't escape the painful bit-twiddling

    Anyway, struct.unpack will undoubtedly be an incredibly valuable help

    thank you very very very much ..

    JiBé
    Jean-Baptiste PERIN, Feb 8, 2005
    #3
  4. Jean-Baptiste PERIN <> wrote:

    > I'll have to handle Intel-PC, DEC-VAX and MIPS-SUN/SGI numbers
    > So I can't escape the painful bit-twiddling


    I don't recall for sure (even though I did my thesis on a Vax, 25 years
    ago!) but I think you _might_ be lucky -- VAX used the binary format
    that became the IEEE standard, if I recall correctly.

    Intel, MIPS, SUN and SGI surely did follow IEEE standards, endianity
    apart, and you can correct for endianity with struct.unpack.

    The problem would be there if you had, say, floats in old IBM 360/370
    formats, or Cray's original formats, or the like...


    > Anyway, struct.unpack will undoubtedly be an incredibly valuable help
    >
    > thank you very very very much ..


    You're welcome!


    Alex
    Alex Martelli, Feb 8, 2005
    #4
  5. Alex Martelli wrote:

    > I don't recall for sure (even though I did my thesis on a Vax, 25 years
    > ago!) but I think you _might_ be lucky -- VAX used the binary format
    > that became the IEEE standard, if I recall correctly.


    iirc, you have to swap bytes around. the code on this page might
    be helpful:

    http://www.octave.org/octave-lists/archive/octave-sources.2004/msg00033.html

    > The problem would be there if you had, say, floats in old IBM 360/370
    > formats, or Cray's original formats, or the like...


    here's a IBM 360 converter (at least that's what I think it is; the code is taken
    from a PIL format converter for a format that uses "IBM floats"):

    def ibm_f32s(c):
    a = ord(c[0]) & 127
    b = ord(c[3]) + (ord(c[2])<<8) + (ord(c[1])<<16)
    v = pow(2.0, -24) * b * pow(16.0, a-64)
    if ord(c[0]) > 127:
    return -v
    return v

    many floating point formats are trivial variations on this theme.

    </F>
    Fredrik Lundh, Feb 8, 2005
    #5
    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. Andy
    Replies:
    7
    Views:
    6,241
    Roedy Green
    May 10, 2004
  2. bd
    Replies:
    0
    Views:
    606
  3. Michael Yanowitz

    Splitting a float into bytes:

    Michael Yanowitz, Jul 26, 2006, in forum: Python
    Replies:
    4
    Views:
    2,906
    John Machin
    Jul 27, 2006
  4. Michael Yanowitz

    RE: Splitting a float into bytes:

    Michael Yanowitz, Jul 26, 2006, in forum: Python
    Replies:
    0
    Views:
    341
    Michael Yanowitz
    Jul 26, 2006
  5. Carsten Fuchs
    Replies:
    45
    Views:
    1,508
    James Kanze
    Oct 8, 2009
Loading...

Share This Page