def varint_compress(zz):
res = []
while zz:
if (zz >> 7) != 0:
res.append(0x80 | (zz & 0x7F))
zz = zz >> 7
else:
res.append(zz & 0x7F)
break
return res 获取 Varint 的长度方法
def varint_code_len(vb):
res = 0
while vb:
vb >>= 7
res += 1
return res Varint 解码实现(Python)
def varint_decompress(zb):
res = 0
offset, size = 0, len(zb)
for i in range(size):
if (zb & 0x80) == 0x80:
res |= (zb & 0x7F) << offset
else:
res |= zb << offset
break
offset += 7
return res 总结一下