在python中读取原始二进制数据并将其转换为ascii

Vin*_*ent 2 python binaryfiles binary-data python-2.7

我有一个原始二进制数据,我想将其转换为可读文本.

文本包含不可读的内容,它还有特殊字符,如带有NUL字的黑盒子或"N - [«'N- [«)>)ÿ".我只是python的新手.

这是我的代码

import struct
file = open('rawbinary.txt')
text = file.read()
struct.unpack("iiiii", text[:20])
Run Code Online (Sandbox Code Playgroud)

我的输出是:

(2113933569, 67305475, -80477197, 1536577129, 1312228259)
Run Code Online (Sandbox Code Playgroud)

如果添加这个:

text[:10]
Run Code Online (Sandbox Code Playgroud)

我的输出是

'\x01\x11\x00~\x03\x00\x03\x04\xf3\x03'
Run Code Online (Sandbox Code Playgroud)

我做得对吗?我下一步要做什么?

mpe*_*kov 5

使用内置的ord功能.

with open("/bin/ls", "rb") as fin:
  buf = fin.read()
bytes = map(ord, buf)    
print bytes[:10]
Run Code Online (Sandbox Code Playgroud)

输出:

[127, 69, 76, 70, 2, 1, 1, 0, 0, 0]
Run Code Online (Sandbox Code Playgroud)