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 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
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]
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:
Comment