매핑 사용 : 여기에 버그 및 비 효율성에서 별도로
# 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 개 항목을 정의하는 당신에게 달려 있습니다.
해밍 코드에는 다양한 형태가 있습니다 (http://en.wikipedia.org/wiki/Hamming_code). 무엇을 성취하려고합니까? 일반적인 접근법은 파일의 각 바이트의 각 니블에 7,4 해밍 코드를 적용하여 거의 두 배 크기의 새 파일을 생성하는 것입니다. 그러나 7,4 코드가 있더라도 인코딩 된 데이터를 나타내는 방법은 사용자에게 달려 있습니다. – Neil
그래, 나는 데이터에 해밍 코드 (7,4)를 시도 할 것이다. 내 초점은 내결함성이므로 파일 크기가 두 배로 늘어도 괜찮습니다. 내 질문은 데이터를 읽는 것이 불가피하거나 파이썬에서 내 목적을 달성 할 수있는 라이브러리 함수가있는 경우입니다. Thanks @Neil – kate
파일을 mmap 할 수 있습니다. –