I have a list of hex numbers I need to convert to string to write it to binary file. How can I do that? (convert the list of hex numbers to a string)
converting to a string is easy
>>> "aabbccddeeff".decode('hex')
'\xaa\xbb\xcc\xdd\xee\xff'
you don't need to do anything special to write this to a file
>>> with open("out.txt", "wb") as f:
... f.write("aabbccddeeff".encode('hex'))
For Python3, it is slightly different
>>> import binascii
>>> with open("out.txt", "wb") as f:
... f.write(binascii.unhexlify("aabbccddeeff"))
...
6
Edit after clarification in the comments:
>>> with open("out.txt", "wb") as f:
... f.write(''.join(['\x00', '\x80', '\xfe', '\x7f']))
Again, this is slightly different in Python3
>>> with open("out.txt", "wb") as f:
... f.write(b''.join([b'\x00', b'\x80', b'\xfe', b'\x7f']))
...
4
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.