Write an hexadecimal file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Cesar Andres Roldan Garcia

    #1

    Write an hexadecimal file

    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
  • Larry Bates

    #2
    Re: Write an hexadecimal file

    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 111100000111100 0111 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:[color=blue]
    > 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!
    >[/color]

    Comment

    • Raymond Hettinger

      #3
      Re: Write an hexadecimal file

      [Cesar Andres Roldan Garcia][color=blue]
      > 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.[/color]

      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.
      [color=blue][color=green][color=darkred]
      >>> import struct, binascii
      >>> binascii.hexlif y(struct.pack(' >f', 3.1415926535))[/color][/color][/color]
      '40490fdb'[color=blue][color=green][color=darkred]
      >>> struct.unpack(' >f', binascii.unhexl ify(_))[0][/color][/color][/color]
      3.1415927410125 732

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

      f = open('mydata.he x', 'w')
      f.write('40490f db')
      f.close()



      Raymond Hettinger


      Comment

      • rbt

        #4
        Re: Write an hexadecimal file

        Larry Bates wrote:[color=blue]
        > There is not such thing as a hexadecimal file.[/color]

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

        Comment

        Working...