how to convert 4 bytes into a float ?

  • Thread starter Jean-Baptiste PERIN
  • Start date
J

Jean-Baptiste PERIN

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 ?
 
A

Alex Martelli

Jean-Baptiste PERIN said:
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
 
J

Jean-Baptiste PERIN

Alex Martelli a écrit :
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é
 
A

Alex Martelli

Jean-Baptiste PERIN said:
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
 
F

Fredrik Lundh

Alex said:
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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top