我正在尝试使用汉明码(python语言)对.txt文件中的数据进行编码。我该怎么办呢?我是否需要逐行读出数据,转换为ascii字符,然后计算汉明码。或者python中是否有任何可以作为窗口的函数或库,可以作为一个整个文件进行操作?
非常感谢您的回复。比你提前。
编辑: 该场景是客户端服务器架构。客户端在计算数据的汉明码后尝试将文件上传到服务器并将其存储在服务器中。稍后,当它尝试检索文件时,它会检查汉明码并检测可能发生的任何错误。
答案 0 :(得分:1)
使用映射:
# create a dict that maps input bytes to their hamming-encoded version. This
# can be pre-calculated and hard-coded, or generated at startup
hamming = {
0x00: 0x0000, # these numbers are nonsense. Input byte 0x00 is
# being mapped to output bytes 0x0000
0x01: 0x0101,
...
0xff: 0x10cf
}
# read the source binary file
with open('input.bin', 'r') as infile:
data = [int(x) for x in infile.read()]
# translate the input data into its encoded form (1 byte becomes 2 with parity added, etc)
output = ''
for byte in data:
encoded = hamming[byte]
output += chr((encoded >> 8) & 0xff)
output += chr((encoded >> 0) & 0xff)
# write the encoded data to a file
with open('output.bin', 'w') as out:
out.write(output)
除了这里的任何错误和效率低下之外,您还可以在dict hamming
中定义256个条目。