Python 2.7将原始字节写入stdout

时间:2018-05-24 10:08:56

标签: python bash python-2.7 hex byte

我需要使用Python 2.7脚本将4个原始十六进制字节写入bash。我碰到了这个看起来很有希望的主题: How to write a raw hex byte to stdout in Python 3? - 但它仅适用于Python 3。 什么是Python 2.7中该方法的等价物?

1 个答案:

答案 0 :(得分:0)

您可以使用binascii.unhexlify

import sys
from binascii import unhexlify as unhex

# Prints out the character 'N' 
sys.stdout.write(unhex('4e'))

# ...

# Prints out Hello, world!
sys.stdout.write(''.join(map(unhex, [
                     '48', '65', '6c', 
                     '6c', '6f', '2c', 
                     '20', '77', '6f', 
                     '72', '6c', '64', 
                     '21'
                ])))
相关问题