Write an hexadecimal file

  • Thread starter Cesar Andres Roldan Garcia
  • Start date
C

Cesar Andres Roldan Garcia

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
 
L

Larry Bates

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
 
R

Raymond Hettinger

[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
 
R

rbt

Larry said:
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 ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top