最近在搞上期所行情解析,第一次遇到vint编码,不知道自己做的low不low,仅供参考。
# vint解码demo
# author: lwh
# date:2021/06/23
# zigzag编码
def zigzag_encode(num):
"""
EncZZ(n) = (n << 1) ^ (n >> 63)
"""
return (num << 1) ^ (num >> 63)
# zigzag解码
def zigzag_decode(num):
"""
DecZZ(n) = (-(n & 0x01)) ^ ((n >> 1) & ~( 1<< 64))
"""
return (-(num & 0x01)) ^(num >> 1) & ~(num << 64)
# varint解码
def varint_decode(bin_str):
"""
索引可