Write an hexadecimal file

Discussion in 'Python' started by Cesar Andres Roldan Garcia, Mar 30, 2005.

  1. 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
    Cesar Andres Roldan Garcia, Mar 30, 2005
    #1
    1. Advertising

  2. Cesar Andres Roldan Garcia

    Larry Bates Guest

    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!
    >
    Larry Bates, Mar 30, 2005
    #2
    1. Advertising

  3. [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
    Raymond Hettinger, Mar 30, 2005
    #3
  4. Cesar Andres Roldan Garcia

    rbt Guest

    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 ;)
    rbt, Mar 30, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dmitry Bond.

    Hexadecimal formatting with leading zeros?!

    Dmitry Bond., Oct 13, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    7,963
    Oliver Sturm
    Oct 13, 2005
  2. Victor Hannak
    Replies:
    0
    Views:
    7,425
    Victor Hannak
    Feb 20, 2004
  3. Vijay

    How to read a Hexadecimal file ?

    Vijay, Aug 7, 2006, in forum: C Programming
    Replies:
    8
    Views:
    617
    Joe Wright
    Aug 8, 2006
  4. Ralf Hildebrandt

    Writing hexadecimal to file

    Ralf Hildebrandt, Mar 6, 2007, in forum: VHDL
    Replies:
    5
    Views:
    670
  5. Replies:
    4
    Views:
    478
Loading...

Share This Page