By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,715 Members | 768 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,715 IT Pros & Developers. It's quick & easy.

Write an hexadecimal file

P: n/a
Hi

I'm trying to write an hexadecimal file... I mean not a text plain...
I have to convert a float decimal number in float hexadecimal one, and
that's done.

That number is the one I'm gonna write in the hex file... can anybody
help me 'cause i don't know how python write an hex file!

Thanks!

--
Atentamente,

Cesar Andres Roldan Garcia
Presidente Comunidad Académica Microsoft Javeriana
Cali - Colombia
Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
There is not such thing as a hexadecimal file. Files are either
binary or text. Hexadecimal is a representation of binary data
where 4 bits are displayed as a single hex digit (0-F) as
shorthand (because 1111000001111000111 is just too difficult to
deal with).

To write binary data to a file in Python you open the file with
"wb" mode.

Example:

fp=open('myfile.dat', 'wb')
fp.write(bytes)
fp.close()

From your post I cannot tell anything more about what you are
actually doing, so I hope this helps.

Larry Bates
Cesar Andres Roldan Garcia wrote:
Hi

I'm trying to write an hexadecimal file... I mean not a text plain...
I have to convert a float decimal number in float hexadecimal one, and
that's done.

That number is the one I'm gonna write in the hex file... can anybody
help me 'cause i don't know how python write an hex file!

Thanks!

Jul 18 '05 #2

P: n/a
[Cesar Andres Roldan Garcia]
I'm trying to write an hexadecimal file... I mean not a text plain...
I have to convert a float decimal number in float hexadecimal one,
and that's done.


The struct module provides a portable way to convert a float to and from a
sequence of bytes.

The binascii modules provides tools for converting a sequence of bytes to and
from a representation as a hex string.
import struct, binascii
binascii.hexlify(struct.pack('>f', 3.1415926535)) '40490fdb' struct.unpack('>f', binascii.unhexlify(_))[0]

3.1415927410125732

Writing to a file is accomplished with the open() function and the file.write()
method:

f = open('mydata.hex', 'w')
f.write('40490fdb')
f.close()

Raymond Hettinger
Jul 18 '05 #3

P: n/a
rbt
Larry Bates wrote:
There is not such thing as a hexadecimal file.


Right, 300 is 300 whether you choose to represent it in decimal, binary,
hex, etc... it's still only 300 of something ;)
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.