如何在python中将"ASCII"转换为"HEX"
我有一个文件需要阅读.但使用下面的代码它只能显示ASCII
with open('Hello.DAT','rb') as f:
data= f.read()
print(data)
Run Code Online (Sandbox Code Playgroud)
它可以以这种格式打印数据:
01201602180000020000000007000000000054000000000000\X0
如何将此数据转换为HEX值,如下所示:
30 31 32 30 31 36 30 32 31 38 30 30 30 30 30 32 30 30 30 30 30 30 30 30 37 30 30 30 30 30 30 30 30 30 30 35 34 30 30 30 30 30 30 30 30 30 30 30 30 5c 78 30
假设python3:
with open('Hello.DAT','rb') as f:
data = f.read()
print(" ".join("{:02x}".format(c) for c in data))
Run Code Online (Sandbox Code Playgroud)
(在python2中更改format(c)为format(ord(c)))