Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > convert binary to float

Reply
Thread Tools

convert binary to float

 
 
Mason
Guest
Posts: n/a
 
      06-01-2008
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 [1]: f = open("test2.pc0", "rb")
In [2]: tagData = f.read(4)
In [3]: tagData
Out[3]: '\x00\x00\xc0@'

I can then do the following in order to convert it to a float:

In [4]: struct.unpack("f", "\x00\x00\xc0@")
Out[4]: (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??):
„@

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?

Thanks,
Mason




 
Reply With Quote
 
 
 
 
George Sakkis
Guest
Posts: n/a
 
      06-01-2008
On Jun 1, 3:55*pm, Mason <john.gerald.ma...@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 [1]: f = open("test2.pc0", "rb")
> * * In [2]: tagData = f.read(4)
> * * In [3]: tagData
> * * Out[3]: '\x00\x00\xc0@'
>
> I can then do the following in order to convert it to a float:
>
> * * In [4]: struct.unpack("f", "\x00\x00\xc0@")
> * * Out[4]: (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

George
 
Reply With Quote
 
 
 
 
Mason
Guest
Posts: n/a
 
      06-01-2008
On Jun 1, 5:12 pm, George Sakkis <george.sak...@gmail.com> wrote:
> On Jun 1, 3:55 pm, Mason <john.gerald.ma...@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 [1]: f = open("test2.pc0", "rb")
> > In [2]: tagData = f.read(4)
> > In [3]: tagData
> > Out[3]: '\x00\x00\xc0@'

>
> > I can then do the following in order to convert it to a float:

>
> > In [4]: struct.unpack("f", "\x00\x00\xc0@")
> > Out[4]: (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
>
> George


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!
 
Reply With Quote
 
Mason
Guest
Posts: n/a
 
      06-01-2008
On Jun 1, 6:41 pm, Dennis Lee Bieber <wlfr...@ix.netcom.com> wrote:
> On Sun, 1 Jun 2008 12:55:45 -0700 (PDT), Mason
> <john.gerald.ma...@gmail.com> declaimed the following in
> comp.lang.python:
>
> > 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 [1]: f = open("test2.pc0", "rb")
> > In [2]: tagData = f.read(4)
> > In [3]: tagData

>
> Interpreter display of raw object name uses repr()
>
> > Out[3]: '\x00\x00\xc0@'

>
> > I can then do the following in order to convert it to a float:

>
> > In [4]: struct.unpack("f", "\x00\x00\xc0@")
> > Out[4]: (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))[0]
> print tagFloat
> --
> Wulfraed Dennis Lee Bieber KD6MOG
> wlfr...@ix.netcom.com wulfr...@bestiaria.com
> HTTP://wlfraed.home.netcom.com/
> (Bestiaria Support Staff: web-a...@bestiaria.com)
> HTTP://www.bestiaria.com/


Thanks Dennis, I'm OK now. I just sort of dropped the ball for a
bit .

Mason
 
Reply With Quote
 
Lie
Guest
Posts: n/a
 
      06-03-2008
On Jun 2, 2:55*am, Mason <john.gerald.ma...@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 [1]: f = open("test2.pc0", "rb")
> * * In [2]: tagData = f.read(4)
> * * In [3]: tagData
> * * Out[3]: '\x00\x00\xc0@'
>
> I can then do the following in order to convert it to a float:
>
> * * In [4]: struct.unpack("f", "\x00\x00\xc0@")
> * * Out[4]: (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??):
> „@
>
> 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?
>
> Thanks,
> Mason


Did you know that '\x00\x00\xc0@' is Python's representation for the
'„@'. A print function calls str() first on what it's going to print,
and thus the binary representation is converted into real characters
(in the range of ASCII). If you've designed your code to process this
representation, then you should call the repr() first, a better
solution would be to design the code to process the binary directly,
python "knows" how to work with it, like if you do a for-loop, it
knows to fetch '\xc0' as one character instead of four.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
float to string to float, with first float == second float Carsten Fuchs C++ 45 10-08-2009 09:47 AM
Convert binary string to float bilbo2000@comcast.net C++ 2 01-26-2007 06:58 PM
need code to convert float format to internal java float format which is kept in 4 bytes integer Andy Java 7 05-10-2004 09:26 PM
how to convert 16-bit binary fraction to a float music4 C Programming 4 09-20-2003 02:59 AM
Re: float->byte->float is same with original float image. why float->ubyte->float is different??? bd C Programming 0 07-07-2003 12:09 AM



Advertisments