Python2.7将原始字节写入stdou

2024-05-08 23:35:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要使用python2.7脚本将4个原始十六进制字节写入bash。我遇到了这样一条似乎很有前途的线索: How to write a raw hex byte to stdout in Python 3?-但它只适用于python3。 在Python2.7中,这个方法的等价物是什么?在


Tags: to方法in脚本bashraw字节stdout
1条回答
网友
1楼 · 发布于 2024-05-08 23:35:57

您可以使用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'
                ])))

相关问题 更多 >