Results 1 to 7 of 7

Thread: Python writing hex/bytes to file

  1. #1
    Join Date
    Oct 2010
    Beans
    8

    Python writing hex/bytes to file

    Hi,

    Right now I'm trying to write a list of hex values (strings) to a disk file in Python 3.1.
    Code:
    ['AB', 'EC', 'CD', 'AB', 'ED', 'EB', 'DB', 'AB', 'EC']
    I tried adding \x before each one and then writing it to file, but it's writing it literally as '\xAB\xEC\xCD\xAB\xED\xEB\xDB\xAB\xEC'.

    What do I need to do to write it as bytes?

    Thanks.

  2. #2
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Python writing hex/bytes to file

    Last edited by Vaphell; December 7th, 2010 at 05:48 AM.

  3. #3
    Join Date
    Mar 2005
    Beans
    947
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Python writing hex/bytes to file

    You might want to post more of your code to illustrate exactly what you've tried.

  4. #4
    Join Date
    Oct 2010
    Beans
    8

    Re: Python writing hex/bytes to file

    I tried that an it spat out:
    Code:
    TypeError: must be bytes or buffer, not str
    Quote Originally Posted by wmcbrine View Post
    You might want to post more of your code to illustrate exactly what you've tried.
    Code:
    bitlist = ['AB', 'EC', 'CD', 'AB', 'ED', 'EB', 'DB', 'AB', 'EC']
    for bit in bitlist:
        bitstring += r'\x' + bit
    bitout = open('bitout', 'wb')
    bitout.write(bitstring)

  5. #5
    Join Date
    Jul 2009
    Location
    London
    Beans
    1,480
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Python writing hex/bytes to file

    the binascii module can help:
    Code:
    import binascii
    bitlist = ['AB', 'EC', 'CD', 'AB', 'ED', 'EB', 'DB', 'AB', 'EC']
    bytes = binascii.a2b_hex(''.join(bitlist))
    bitout.write(bytes)

  6. #6
    Join Date
    Dec 2004
    Location
    Manchester
    Beans
    2,086
    Distro
    Ubuntu Mate 15.10 Wily Werewolf

    Re: Python writing hex/bytes to file

    if those byte represent values you may find http://docs.python.org/library/struct.html useful

  7. #7
    Join Date
    Mar 2005
    Beans
    947
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Python writing hex/bytes to file

    Quote Originally Posted by v1rati View Post
    Code:
    bitlist = ['AB', 'EC', 'CD', 'AB', 'ED', 'EB', 'DB', 'AB', 'EC']
    for bit in bitlist:
        bitstring += r'\x' + bit
    bitout = open('bitout', 'wb')
    bitout.write(bitstring)
    So, what you're doing here is creating strings that start with a literal backslash. These are not then evaluated into byte values. (Think about it -- at what point would that happen?)

    You might try something more like this:

    Code:
    for bit in bitlist:
        bitstring += chr(int(bit, 16))

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •