how to convert 4 bytes into a float ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jean-Baptiste PERIN

    #1

    how to convert 4 bytes into a float ?


    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 ?

  • Alex Martelli

    #2
    Re: how to convert 4 bytes into a float ?

    Jean-Baptiste PERIN <jb_perin@yahoo .fr> wrote:
    [color=blue]
    > 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 ?[/color]

    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

    Comment

    • Jean-Baptiste PERIN

      #3
      Re: how to convert 4 bytes into a float ?

      Alex Martelli a écrit :
      [color=blue]
      > Jean-Baptiste PERIN <jb_perin@yahoo .fr> wrote:
      >
      >[color=green]
      >>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 ?[/color]
      >
      >
      > 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[/color]

      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é

      Comment

      • Alex Martelli

        #4
        Re: how to convert 4 bytes into a float ?

        Jean-Baptiste PERIN <jb_perin@yahoo .fr> wrote:
        [color=blue]
        > I'll have to handle Intel-PC, DEC-VAX and MIPS-SUN/SGI numbers
        > So I can't escape the painful bit-twiddling[/color]

        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...

        [color=blue]
        > Anyway, struct.unpack will undoubtedly be an incredibly valuable help
        >
        > thank you very very very much ..[/color]

        You're welcome!


        Alex

        Comment

        • Fredrik Lundh

          #5
          Re: how to convert 4 bytes into a float ?

          Alex Martelli wrote:
          [color=blue]
          > 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.[/color]

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


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

          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>



          Comment

          Working...