转换为字符串很容易
>>> "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)