0

.txt ファイルに存在するデータをハミング コード (python 言語) でコーディングしようとしています。どうすればそれについて行くことができますか?データを 1 行ずつ読み取り、ASCII 文字に変換してから、ハミング コードを計算する必要があります。または、ウィンドウとして機能し、ファイル全体を 1 つとして操作できる 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 に答える