Skip to content

Instantly share code, notes, and snippets.

@meeuw
Created June 18, 2023 18:53
Show Gist options
  • Save meeuw/131067f821138389ceb6d8db797fe3b1 to your computer and use it in GitHub Desktop.
Save meeuw/131067f821138389ceb6d8db797fe3b1 to your computer and use it in GitHub Desktop.
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