python十六进制数列表字符串和写入文件?

-1 python string hex file list

我有一个十六进制数列表,我需要转换为字符串,将其写入二进制文件.我怎样才能做到这一点?(将十六进制数列表转换为字符串)

Joh*_*ooy 9

转换为字符串很容易

>>> "aabbccddeeff".decode('hex')
'\xaa\xbb\xcc\xdd\xee\xff'
Run Code Online (Sandbox Code Playgroud)

你不需要做任何特别的事情来把它写到文件中

>>> with open("out.txt", "wb") as f:
...     f.write("aabbccddeeff".encode('hex'))
Run Code Online (Sandbox Code Playgroud)

对于Python3,它略有不同

>>> import binascii
>>> with open("out.txt", "wb") as f:
...     f.write(binascii.unhexlify("aabbccddeeff"))
... 
6
Run Code Online (Sandbox Code Playgroud)

在评论中澄清后编辑:

>>> with open("out.txt", "wb") as f:
...     f.write(''.join(['\x00', '\x80', '\xfe', '\x7f']))
Run Code Online (Sandbox Code Playgroud)

同样,这在Python3中略有不同

>>> with open("out.txt", "wb") as f:
...     f.write(b''.join([b'\x00', b'\x80', b'\xfe', b'\x7f']))
... 
4
Run Code Online (Sandbox Code Playgroud)