VB encoding, Gamma encoding, Delta encoding
Study notes, record VB coding, Gamma coding, Delta coding automatic calculation code.
1. VB code
Example: The decimal number 824 is encoded with vb code
(1) The decimal number 824 is converted to hexadecimal 338
(2) Each digit in the hexadecimal number is treated as a single decimal number and then converted into binary four digits. Then concatenate 0011 0011 1000
(3) Remove all 0s at the beginning to get 1100111000
(4) Split the string into 7 groups by adding 0 at the beginning to get 0000110 and 0111000
(5) In the last 7 digits Add 1 in front of the number, and add 0 in front of the rest of the 7 digits that do not represent the termination, and make up a group of 8.
(6) The final result is:
00000110 10111000
Encoding code, input raw digital output string:
def vb_encoding(ori_data):
code = hex(ori_data)[2:]
result = ''
for i in code:
temp = bin(int(i,10))[2:]
if len(temp) < 4:
temp = '0'*(4-len(temp)) + temp
result = result + temp
# 去除开头的0
result = result.lstrip('0')
# 补全到7的倍数
if len(result) % 7 != 0:
result = '0'*(7-len(result) % 7) + result
# 拆分为多行
result_list = []
result_list.append('0' + result[:7])
while len(result) != 7:
result = result[7:]
result_list.append('0' + result[:7])
result_list[-1] = '1' + result_list[-1][1:]
return result_list
Decode the code, enter the binary string to get multiple original numbers:
def vb_decoding(bin_data):
bin_data = bin_data.replace(" ", "")
result = []
while len(bin_data) != 0:
t = 0
while bin_data[t*8] != '1':
t += 1
# 获取当前数字的字符串
if len(bin_data) == (t+1)*8:
cut = bin_data
bin_data = ''
else:
cut = bin_data[:(t+1)*8]
bin_data = bin_data[(t+1)*8:]
# 开始还原数字
code = []
for i in range(len(cut)//8-1):
code.append(cut[i*8: (i+1)*8])
code.append(cut[-8:])
code = list(map(lambda x:x[1:], code))
code = ''.join(code)
code = code.lstrip('0')
if len(code) % 4 != 0:
code = '0' * (4 - len(code) % 4) + code
temp_bin = []
for i in range(len(code)//4-1):
temp_bin.append(code[i*4: (i+1)*4])
temp_bin.append(code[-4:])
temp_bin = list(map(lambda x:str(int(x,2)), temp_bin))
number = int(''.join(temp_bin), 16)
result.append(number)
return result
2. Gamma (γ) code
Gamma coding rounds down
the number k
(1) kd = (log2(K))
(2) kr = K-2^kd
kd is expressed as unary, that is, kd 1 plus a 0
kr is expressed as binary, That is, kr is expressed in binary code
Encoding code, input raw digital output string:
def gamma_encoding(ori_data):
kd = math.floor(math.log2(ori_data))
kr = ori_data - 2**kd
result = '1'*kd+'0'+bin(kr)[2:]
return result
Decode the code, enter the binary string to get multiple original numbers:
def gamma_decoding(bin_data):
bin_data = bin_data.replace(" ", "")
number_list = []
while len(bin_data) != 0:
index_of_zero = None
# 找到第一个0的位置
for i in range(len(bin_data)):
if bin_data[i] == '0':
index_of_zero = i
break
if index_of_zero == None:
return "error string"
# 如果第一个就是0
if index_of_zero == 0:
number_list.append(1)
bin_data = bin_data[1:]
# 否则
else:
bin_data = bin_data[index_of_zero + 1:]
kd = index_of_zero
tran_kr = bin_data[:kd]
kr = int(tran_kr, 2)
bin_data = bin_data[kd:]
ori_num = 2 ** kd + kr
number_list.append(ori_num)
return number_list
3. Delta (δ) code
On the basis of Gamma coding, perform another gamma coding on kd to obtain kdd and kdr, thereby further shortening the length of the coding.
Encoding code, input raw digital output string:
def delta_encoding(ori_data):
kd = math.floor(math.log2(ori_data))
kr = ori_data - 2**kd
tran_kr = bin(kr)[2:]
# 因为后面kd要+1,所以这里先编码完最后一段
if len(tran_kr) < kd:
tran_kr = '0'*(kd-len(tran_kr)) + tran_kr
kd = kd + 1
kdd = math.floor(math.log2(kd))
kdr = kd - 2**kdd
tran_kdd = '1'*kdd + '0'
tran_kdr = bin(kdr)[2:]
if len(tran_kdr) < kdd:
tran_kdr = '0'*(kdd-len(tran_kdr)) + tran_kdr
result = tran_kdd + ' ' + tran_kdr + ' ' + tran_kr
#print("kdd:", kdd, ", kdr:", kdr, ", kd:",kd,", kr:", kr)
return result
Decode the code, enter the binary string to get multiple original numbers:
def delta_decoding(bin_data):
bin_data = bin_data.replace(" ", "")
number_list = []
while len(bin_data) != 0:
index_of_zero = None
# 找到第一个0的位置
for i in range(len(bin_data)):
if bin_data[i] == '0':
index_of_zero = i
break
if index_of_zero == None:
return "error string"
# 如果第一个就是0
if index_of_zero == 0:
number_list.append(1)
bin_data = bin_data[1:]
# 否则
else:
bin_data = bin_data[index_of_zero + 1:]
kdd = index_of_zero
tran_kdr = bin_data[:kdd]
kdr = int(tran_kdr,2)
bin_data = bin_data[kdd:]
kd = 2**kdd + kdr - 1
tran_kr = bin_data[:kd]
kr = int(tran_kr,2)
bin_data = bin_data[kd:]
ori_num = 2**kd + kr
number_list.append(ori_num)
return number_list