Skip to content

Instantly share code, notes, and snippets.

@johndobrien
Created July 24, 2012 14:25
Show Gist options
  • Save johndobrien/3170211 to your computer and use it in GitHub Desktop.
Save johndobrien/3170211 to your computer and use it in GitHub Desktop.
encode a data stream with a 7bit encoded variant
def decode7bit(bytes):
"""Decode the first variant integer in bytes.
This function assumes that bytes begins with an encoded variant of an
integer. It will read one byte at a time until the high order bit is 0 and
the combined value of 7bit integers values will be returned along with how
many bytes have been read.
"""
pos = 0
value = 0
shift = 0
bytes_read = 1
while True:
byteval = ord(bytes[pos])
if(byteval & 128) == 0: break
bytes_read += 1
pos += 1
value |= ((byteval & 0x7F) << shift)
shift += 7
return (value | (byteval << shift)), bytes_read
def encode7bit(value):
"""Return a variant of value in bytes.
Each byte is encoded with a 7 bit integer and the high order bit used
for continuation to the next byte as needed. The last byte will not have
The continuation bit set.
"""
temp = value
bytes = ""
while temp >= 128:
bytes += chr(0x000000FF & (temp | 0x80))
temp >>= 7
return bytes + chr(temp)
header = "header"
data = "And this is some data..."
# first byte(s) are a version of the data
ver = encode7bit(199)
# next encode the size of the header
ds = encode7bit(len(header))
# put them together with the data
x = ver + ds + header + data
# read it back
ver, ver_br = decode7bit(x)
hs, hs_br = decode7bit(x[ver_br:])
header = x[ver_br+hs_br:ver_br+hs_br+hs]
data = x[ver_br+hs_br+hs:]
print "Version:%r" % ver
print "Header:%r" % header
print "Data:%r" % data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment