convert binary to float
john.gerald.mason at gmail.com
Sun Jun 1 23:52:32 CEST 2008
On Jun 1, 5:12 pm, George Sakkis <george.sak... at gmail.com> wrote:
> On Jun 1, 3:55 pm, Mason <john.gerald.ma... at gmail.com> wrote:
> > 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
> > 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
> > I get this (ASCII??):
> > „@
> Remembering to put that struct.unpack() call in your module might
> help ;-)
Wow ... I did have it in there, but I forgot include it in my post.
Anyway, this works just fine:
f = open("test2.pc0", "rb")
tagData = f.read(4)
print struct.unpack("f", tagData)
Thanks for waking me up George!
More information about the Python-list