2013-11-26 50 views
0

我想在漢明碼(python語言)中編碼.txt文件中的數據。我怎麼去解決它?我是否需要逐行讀出數據,轉換成ASCII字符,然後計算漢明碼。或者是否有Python中的任何函數或庫可以作爲一個窗口,並可以在整個文件中作爲一個整體進行操作?在文件上計算漢明碼

您的回覆非常感謝。比你提前。

編輯: 該場景是一個客戶端服務器體系結構。在計算數據的漢明碼並將其存儲在服務器中後,客戶端會嘗試上傳文件到服務器。稍後,當它嘗試恢復文件時,它會檢查漢明碼並檢測可能發生的任何錯誤。

+0

海明碼有很多種形式(http://en.wikipedia.org/wiki/Hamming_code)。你想達到什麼目的?一種常用的方法可能是對文件中每個字節的每個nybble應用7,4漢明碼,這會產生幾乎兩倍大小的新文件。但是,即使使用7,4代碼,如何表示編碼數據也取決於您。 – Neil

+0

是的,我會嘗試(7,4)海明碼的數據。我的重點是容錯,所以我不介意將文件大小加倍。我的問題是,如果讀出數據是不可避免的,或者如果Python中有任何庫函數可以滿足我的目的。謝謝@Neil – kate

+0

您可以mmap文件 –

回答

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) 
這裏的任何錯誤和低效

除此之外,它只是給你在字典hamming定義256個條目。