how to convert 4 bytes into a float ?
Fredrik Lundh
fredrik at pythonware.com
Tue Feb 8 07:23:11 EST 2005
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>
More information about the Python-list
mailing list