473,666 Members | 2,075 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 22209
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
8333
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
23161
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
13693
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
10711
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
4120
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
46427
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
5969
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
7232
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
6281
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
8448
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8783
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
8552
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
8640
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6198
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
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...
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1776
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.