Created
June 18, 2023 18:53
-
-
Save meeuw/131067f821138389ceb6d8db797fe3b1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def chunk_bytes(value, bit_length): | |
""" | |
Chunk value into bytes of bit_length | |
""" | |
for _ in range(0, value.bit_length(), bit_length): | |
yield value & ((1 << bit_length) - 1) | |
value >>= bit_length | |
def one_is_more_encode(value): | |
""" | |
Encode value into bytes with 7 bits of payload and 1 bit | |
which indicates the continuation. | |
""" | |
result = bytearray() | |
first = True | |
for byte in chunk_bytes(value, 7): | |
if first: | |
first = False | |
else: | |
result[-1] ^= 1 << 7 | |
result.append(byte) | |
return bytes(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment