【发布时间】:2018-10-09 20:11:07
【问题描述】:
我需要从二进制数据流中提取一些数值。 下面的代码对我有用,但可以肯定的是,在 python 中有更合适的方法可以做到这一点。特别是我一直在努力寻找一种更好的方法来迭代数组并从缓冲区中获取 4 个字节作为字节数组。
给我一些提示?
outfile = io.BytesIO()
outfile.writelines(some binary data stream)
buf = outfile.getvalue()
blen = int(len(buf) / 4 );
for i in range(blen):
a = bytearray([0,0,0,0])
a[0] = buf[i*4]
a[1] = buf[i*4+1]
a[2] = buf[i*4+2]
a[3] = buf[i*4+3]
data = struct.unpack('<l', a)[0]
do something with data
【问题讨论】: