convert binary to float
john.gerald.mason at gmail.com
Sun Jun 1 23:58:43 CEST 2008
On Jun 1, 6:41 pm, Dennis Lee Bieber <wlfr... at ix.netcom.com> wrote:
> On Sun, 1 Jun 2008 12:55:45 -0700 (PDT), Mason
> <john.gerald.ma... at gmail.com> declaimed the following in
> > I have tried and tried...
> > I'd like to read in a binary file, convert it's 4 byte values into
> > floats, and then save as a .txt file.
> > This works from the command line (import struct);
> > In : f = open("test2.pc0", "rb")
> > In : tagData = f.read(4)
> > In : tagData
> Interpreter display of raw object name uses repr()
> > Out: '\x00\x00\xc0@'
> > I can then do the following in order to convert it to a float:
> > In : struct.unpack("f", "\x00\x00\xc0@")
> > Out: (6.0,)
> > But when I run the same code from my .py file:
> > f = open("test2.pc0", "rb")
> > tagData = f.read(4)
> > print tagData
> Display from a print statement uses str()
> > I get this (ASCII??):
> > „@
> Probably not ASCII -- ASCII doesn't have that spanish (?) bottom row
> quote... And a pair of null bytes don't take up screen space.
> > I only know how to work with '\x00\x00\xc0@'.
> > I don't understand why the output isn't the same. I need a solution
> > that will allow me to convert my binary file into floats. Am I close?
> > Can anyone point me in the right direction?
> Why do you have to /see/ the byte representation in the first
> place... just feed the four bytes to the struct module directly.
> import struct
> fin = open("test2.pc0", "rb")
> tagFloat = struct.unpack("f", fin.read(4))
> print tagFloat
> Wulfraed Dennis Lee Bieber KD6MOG
> wlfr... at ix.netcom.com wulfr... at bestiaria.com
> (Bestiaria Support Staff: web-a... at bestiaria.com)
Thanks Dennis, I'm OK now. I just sort of dropped the ball for a
More information about the Python-list