tl;dr: Given x,i = b'', 10, how can I concatenate i onto x, resulting in x == b'\x10'?
I'm trying to encode a number in Python as a varint as a header for a protobuf encoding.
Here's the code I have:
def encode_varint(value):
buf = b''
while True:
byte = value & 0x7f
value >>= 7
if value:
buf += chr(byte | 0x80)
else:
buf += chr(byte)
break
return buf
However, this fails because I cannot append a string to bytes.
How do I efficiently take an integer value and append it to a binary string?
buf += chr(byte).encode('ascii'), but that does not smell efficient.