0

我正在尝试用汉明代码(python 语言)对 .txt 文件中存在的数据进行编码。我该怎么办?我需要逐行读出数据,转换成ascii字符,然后在上面计算汉明码。或者python中是否有任何函数或库可以作为一个窗口并且可以对整个文件进行操作?

非常感谢您的回复。比你提前。

编辑:场景是客户端服务器架构。客户端在计算数据的汉明码后尝试将文件上传到服务器并将其存储在服务器中。稍后,当它尝试取回文件时,它会检查汉明码并检测可能发生的任何错误。

4

1 回答 1

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 中定义 256 个条目hamming

于 2013-11-28T02:15:58.257 回答