如何在 python3 中创建二进制文件序列?
How to create sequence of binary file in python3?
我想在 python3
中制作二进制文件序列。以下代码生成第一个。
with open('out', 'wb') as f:
f.write(b'\x00\x00\x00\x00\x00\x00\x00\x00')
结果是
xxd out
0000000: 0000 0000 0000 0000 ........
我想生成包含从 0000 0000 0000 0000
到 ffff ffff ffff ffff
的序列值之一的文件。
如何完成以上任务?
import struct
for i in range(0xffffffffffffffff + 1):
with open('out{:d}'.format(i), 'wb') as f:
f.write(struct.pack('Q', i))
以下将创建一个名为 out
的文件,其中包含序列中可能的 0x10000000000000000 个不同的 8 字节值之一(包括 randint
):
import random
import struct
value = random.randint(0, 0xffffffffffffffff)
hexbytes = ' '.join(''.join(g) for g in zip(*[iter('{:016X}'.format(value))]*4))
print('file contains bytes {}'.format(hexbytes))
with open('out', 'wb') as f:
f.write(struct.pack('Q', value))
示例输出:
file contains bytes EFB4 A2DC A11B 98AE
我想在 python3
中制作二进制文件序列。以下代码生成第一个。
with open('out', 'wb') as f:
f.write(b'\x00\x00\x00\x00\x00\x00\x00\x00')
结果是
xxd out
0000000: 0000 0000 0000 0000 ........
我想生成包含从 0000 0000 0000 0000
到 ffff ffff ffff ffff
的序列值之一的文件。
如何完成以上任务?
import struct
for i in range(0xffffffffffffffff + 1):
with open('out{:d}'.format(i), 'wb') as f:
f.write(struct.pack('Q', i))
以下将创建一个名为 out
的文件,其中包含序列中可能的 0x10000000000000000 个不同的 8 字节值之一(包括 randint
):
import random
import struct
value = random.randint(0, 0xffffffffffffffff)
hexbytes = ' '.join(''.join(g) for g in zip(*[iter('{:016X}'.format(value))]*4))
print('file contains bytes {}'.format(hexbytes))
with open('out', 'wb') as f:
f.write(struct.pack('Q', value))
示例输出:
file contains bytes EFB4 A2DC A11B 98AE