1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| def int8_to_bytes(int8):
"""
Return a bytes string made from the given sequence of [0..255] integers.
"""
# XXX Why in hell python does not feature such conversion???
# XXX Just ignores ints out of bounds.
hex_s = "".join(("{:0>2x}".format(i) for i in int8 if 0 <= i <= 255))
return bytes.fromhex(hex_s)
def gen_nbits(n):
yield(int8_to_bytes((0,)))
for i in range(1, 2**n):
# Note that this will give us lsb first, so well have to reverse that list at the end.
res = []
next = i
while next:
res.append(next % 256)
next //= 256
yield(int8_to_bytes(reversed(res)))
# Two bytes only! Dont want to kill my computer!!
for b in gen_nbits(16):
print(b) |
Partager