Skip to content

Instantly share code, notes, and snippets.

@Elfsong Elfsong/V_Byte_V1.py
Created Mar 25, 2019

Embed
What would you like to do?
V_Byte#python
def vbyte_encode(num):
# out_bytes stores a list of output bytes encoding the number
out_bytes = []
###
# Your answer BEGINS HERE
###
while num >= 128:
out_bytes = [num % 128] + out_bytes
num = num >> 7
out_bytes = [num + 128] + out_bytes
###
# Your answer ENDS HERE
###
return out_bytes
def vbyte_decode(input_bytes, idx):
# x stores the decoded number
x = 0
# consumed stores the number of bytes consumed to decode the number
consumed = 0
###
# Your answer BEGINS HERE
###
x = int("".join(input_bytes[0]),2) - 128
for item in input_bytes[1:]:
x = x * 128 + int("".join(item),2)
consumed = len(input_bytes)
###
# Your answer ENDS HERE
###
return x, consumed
def vbyte_encode(num):
# out_bytes stores a list of output bytes encoding the number
out_bytes = []
###
# Your answer BEGINS HERE
###
while num >= 128:
out_bytes = [num % 128] + out_bytes
num = num >> 7
out_bytes = [num + 128] + out_bytes
###
# Your answer ENDS HERE
###
return out_bytes
def vbyte_decode(input_bytes, idx):
# x stores the decoded number
x = 0
# consumed stores the number of bytes consumed to decode the number
consumed = 0
###
# Your answer BEGINS HERE
###
x = input_bytes[0] - 128
for item in input_bytes[1:]:
x = x * 128 + item
consumed = len(input_bytes)
###
# Your answer ENDS HERE
###
return x, consumed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.