473,856 Members | 1,577 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

convert binary to float

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


Jun 1 '08 #1
4 22424
On Jun 1, 3:55*pm, Mason <john.gerald.ma ...@gmail.comwr ote:
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
Jun 1 '08 #2
On Jun 1, 5:12 pm, George Sakkis <george.sak...@ gmail.comwrote:
On Jun 1, 3:55 pm, Mason <john.gerald.ma ...@gmail.comwr ote:
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!
Jun 1 '08 #3
On Jun 1, 6:41 pm, Dennis Lee Bieber <wlfr...@ix.net com.comwrote:
On Sun, 1 Jun 2008 12:55:45 -0700 (PDT), Mason
<john.gerald.ma ...@gmail.comde claimed the following in
comp.lang.pytho n:
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.netc om.com wulfr...@bestia ria.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
Jun 1 '08 #4
>"George Sakkis" <ge***********@ gmail.comwrote in message
>news:829b1e8 f-baac-4ff4-909b->b3**********@d 45g2000hsc.goog legroups.com...
On Jun 1, 3:55 pm, Mason <john.gerald.ma ...@gmail.comwr ote:
>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
tagData still contains your data, but it is being displayed two different
ways. Consult the documentation about str() and repr().

-Mark

Jun 1 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
8361
by: Jean-Baptiste PERIN | last post by:
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 ?
4
23177
by: jaijai_kumar | last post by:
Select Cast('100.1234' as float) give me the result 100.1234 Now when I convert it back to char I want exactly 100.1234 Select Convert(char(100),Cast('100.1234' as float)) Gives me 100.123 (Here I was expecting 100.1234) When I do Select STR(Cast('100.1234' as float),25,4) I get back the result as 100.1234
2
13713
by: respect | last post by:
i want to convert real float number to 32-bit IEEE binary format in C++ ? plz help me in finding the codes as fast as u can ] Example for input -6.5, the output should be. 1 10000001 101000000000000000000000 0 1 8 9 31 S = 1 E = 129 F = 1.101
4
10721
by: music4 | last post by:
Greetings, I psinfo_t struct, pr_pctcpu is ushort_t type with comment: "pr_pctcpu is 16 bit binary fractions in the range 0.0 to 1.0 with the binary point to the right of the high-orfer bit (1.0=0x8000)." My question is how to convert it to a float? Thanks in advance! Evan
8
4131
by: Ronin | last post by:
I'm very new to C++ coming in from using C# and the like. I've read a lot of good posts about binary files but I'm still missing a few key ingredients that will help me with the following problem. I have a binary file that has a specific number of floats per row. I need to read each float into a float array for processing by another block of code (calculations). Each row gets read into this array and processed before the next row gets...
7
46655
by: ayaniv | last post by:
how to convert * WITHOUT USING ANY FUNCTION * a float number to binary? at least if someone can explain me how to do it it would be better then giving me the code itself.
2
6017
by: bilbo2000 | last post by:
I am trying to read in a binary file which has the data stored as 32 bit float. I have been trying to figure out how to read this in as 32 bit but everytime I try to read it in the best I can do is a 8 bit integer. I was able to do this using a scripting language php, and found that the data is stored in little endian format, so I would read 4 characters in and reverse the string and declare it a float and the number would come out...
7
7270
by: Mario M. Mueller | last post by:
Hi, I have a binary file containing 3 byte float values (big endian). How can I read them into python? The struct module does not work, since it expects 4 byte floats. Any hints? Mario
7
6311
by: chelle2100 | last post by:
hi... i need to write a converter to convert the text file to a binary file with the following file format... CharacterCount:Unsigned Char(This stores the number of characters that is in the file) Name:Char(An array of 256 char that stores the name of the character. You will need to prompt the user if the name given in the text file is longer than the maximum length it can hold) Weapon:Unsigned Short(Uses a single 16bit data type to store...
0
9765
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11065
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10700
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10795
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9536
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5763
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5962
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4177
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3203
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.