使用映射:
# 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)
這裏的任何錯誤和低效
除此之外,它只是給你在字典hamming
定義256個條目。
海明碼有很多種形式(http://en.wikipedia.org/wiki/Hamming_code)。你想達到什麼目的?一種常用的方法可能是對文件中每個字節的每個nybble應用7,4漢明碼,這會產生幾乎兩倍大小的新文件。但是,即使使用7,4代碼,如何表示編碼數據也取決於您。 – Neil
是的,我會嘗試(7,4)海明碼的數據。我的重點是容錯,所以我不介意將文件大小加倍。我的問題是,如果讀出數據是不可避免的,或者如果Python中有任何庫函數可以滿足我的目的。謝謝@Neil – kate
您可以mmap文件 –