Untitled
unknown
plain_text
a year ago
1.1 kB
8
Indexable
codebook = codec.get_code_table() # 获取码书
byteorder = 'little' # 设置字节序
header = bytearray(2) # 创建头部,前两个字节是头部长度,暂时填0
header.append(len(codebook)-1) # 第三个字节为编码表长度
header.extend(len(source).to_bytes(4, byteorder)) # 第四到第七个字节为源文件长度,按小端序排列
for symbol, (word_len, word) in codebook.items(): # 对于码书中的每个符号,码字长度,码字
(word_len, word) = codebook[symbol] # 获取码字长度,码字
word_bytes = int(np.ceil(word_len / 8)) # 计算码字需要的字节数
header.append(symbol) # 添加符号
header.append(word_len) # 添加码字长度
header.extend(word.to_bytes(word_bytes, byteorder)) # 添加码字
header[0:2] = len(header).to_bytes(2, byteorder) # 计算实际头部长度,并填入头部前两个字节Editor is loading...
Leave a Comment