胡乱修改一通
This commit is contained in:
@@ -652,6 +652,8 @@ class coder(QObject):
|
|||||||
for i in range(num):
|
for i in range(num):
|
||||||
d.append(i&0xff)
|
d.append(i&0xff)
|
||||||
d+=self.calc_shell_code(id+i)
|
d+=self.calc_shell_code(id+i)
|
||||||
|
# 查看数据统计
|
||||||
|
prot.huffman_encode(d)
|
||||||
return d
|
return d
|
||||||
# 发送检测命令
|
# 发送检测命令
|
||||||
def cmd_check(self,num:int):
|
def cmd_check(self,num:int):
|
||||||
|
@@ -559,15 +559,65 @@ def huffman(data:bytearray):
|
|||||||
index_table.append((i,count_table[i]))
|
index_table.append((i,count_table[i]))
|
||||||
# print(index_table)
|
# print(index_table)
|
||||||
sort_table(index_table)
|
sort_table(index_table)
|
||||||
# print(index_table)
|
print("index_table",index_table)
|
||||||
num=len(index_table)
|
num=len(index_table)
|
||||||
# clac_zl_table(num)
|
# clac_zl_table(num)
|
||||||
ret=bytearray()
|
ret=bytearray()
|
||||||
for i in index_table:
|
for i in index_table:
|
||||||
ret.append(i[0])
|
ret.append(i[0])
|
||||||
return ret
|
ret.append(i[1])
|
||||||
|
return index_table
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# 获取树的值
|
||||||
|
def calc_sum_of_tree(tree:list):
|
||||||
|
sum=0
|
||||||
|
if(len(tree)==2):
|
||||||
|
sum=calc_sum_of_tree(tree[0])+calc_sum_of_tree(tree[1])
|
||||||
|
elif(len(tree)==1):
|
||||||
|
sum=tree[0][1]
|
||||||
|
return sum
|
||||||
|
|
||||||
|
# 获取树的深度
|
||||||
|
def calc_deep_of_tree(tree:list):
|
||||||
|
deep=1
|
||||||
|
if(len(tree)==2):
|
||||||
|
sum1=calc_deep_of_tree(tree[0])
|
||||||
|
sum2=calc_deep_of_tree(tree[1])
|
||||||
|
if(sum1>sum2):
|
||||||
|
deep+=sum1
|
||||||
|
else:
|
||||||
|
deep+=sum2
|
||||||
|
elif(len(tree)==1):
|
||||||
|
deep=1
|
||||||
|
return deep
|
||||||
|
|
||||||
|
# 在树中找到指定字符的编码
|
||||||
|
def calc_code_of_char(tree:list,code:int):
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# 根据index_table生成huffman树
|
||||||
|
def creat_huffman_tree(index_table:bytearray):
|
||||||
|
index_list=[]
|
||||||
|
for i in range(len(index_table)//2):
|
||||||
|
index_list.append(index_table[i*2:i*2+2])
|
||||||
|
print("index_list",index_list)
|
||||||
|
tree=[index_list[-1]]
|
||||||
|
index_list=index_list[:-1]
|
||||||
|
while len(index_list)>0:
|
||||||
|
sub=[index_list[-1]]
|
||||||
|
index_list=index_list[:-1]
|
||||||
|
# 大在左,小在右
|
||||||
|
if(calc_sum_of_tree(sub)>calc_sum_of_tree(tree)):
|
||||||
|
tree=[sub,tree]
|
||||||
|
else:
|
||||||
|
tree=[tree,sub]
|
||||||
|
print("tree deep:",calc_deep_of_tree(tree))
|
||||||
|
print(tree)
|
||||||
|
return tree
|
||||||
|
|
||||||
# 根据值获取其序号
|
# 根据值获取其序号
|
||||||
def value_to_index(index_table:list,value):
|
def value_to_index(index_table:list,value):
|
||||||
for i in range(len(index_table)):
|
for i in range(len(index_table)):
|
||||||
@@ -585,13 +635,6 @@ def calc_mask(num:int):
|
|||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
|
||||||
# 把指定bit位数的数据添加进数组 bit_count<8
|
|
||||||
def array_add_bit(data:bytearray,bit_index:int,dat:int,bit_count:int):
|
|
||||||
off=bit_index%8
|
|
||||||
data[bit_index//8]|=((calc_mask(bit_count)&dat)<<off)&0xff
|
|
||||||
data[bit_index//8+1]|=((calc_mask(bit_count)&dat)>>(8-off))&0xff
|
|
||||||
bit_index+=bit_count
|
|
||||||
return data,bit_index
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -599,20 +642,9 @@ def huffman_encode(data:bytearray):
|
|||||||
ret=bytearray(1024)
|
ret=bytearray(1024)
|
||||||
index=0
|
index=0
|
||||||
index_table=huffman(data)
|
index_table=huffman(data)
|
||||||
zl_table,count,diff=clac_zl_table(len(index_table))
|
# print("index_table",index_table.hex(' '))
|
||||||
print(index_table)
|
print("index_table",index_table)
|
||||||
print(zl_table)
|
creat_huffman_tree(index_table)
|
||||||
print(count,diff)
|
|
||||||
for i in data:
|
|
||||||
pos=value_to_index(index_table,i)
|
|
||||||
mask_bit_count=count
|
|
||||||
if(pos<diff):
|
|
||||||
mask_bit_count-=1
|
|
||||||
ret,index=array_add_bit(ret,index,zl_table[pos],mask_bit_count)
|
|
||||||
print(ret[0:(index+7)//8].hex(' '))
|
|
||||||
ret=bytearray([len(index_table)])+index_table+bytearray([8-index%8])+ret[0:(index+7)//8]
|
|
||||||
print(ret.hex(' '))
|
|
||||||
return ret
|
|
||||||
|
|
||||||
def huffman_decode(data:bytearray):
|
def huffman_decode(data:bytearray):
|
||||||
zl_table,count,diff=clac_zl_table(data[0])
|
zl_table,count,diff=clac_zl_table(data[0])
|
||||||
|
Reference in New Issue
Block a user