-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHammingString.py
More file actions
162 lines (117 loc) · 4.57 KB
/
HammingString.py
File metadata and controls
162 lines (117 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# thanks for the function definitions to github community
from typing import List
from math import log2, ceil
from random import randrange
def __hamming_common(src: List[List[int]], s_num: int, encode=True) -> None:
"""
Here's the real magic =)
"""
s_range = range(s_num)
for i in src:
sindrome = 0
for s in s_range:
sind = 0
for p in range(2 ** s, len(i) + 1, 2 ** (s + 1)):
for j in range(2 ** s):
if (p + j) > len(i):
break
sind ^= i[p + j - 1]
if encode:
i[2 ** s - 1] = sind
else:
sindrome += (2 ** s * sind)
if (not encode) and sindrome:
i[sindrome - 1] = int(not i[sindrome - 1])
def hamming_encode(msg: str, mode: int=8) -> str:
"""
Encoding the message with Hamming code.
:param msg: Message string to encode
:param mode: number of significant bits
:return:
"""
result = ""
msg_b = msg.encode("utf-8")
s_num = ceil(log2(log2(mode + 1) + mode + 1)) # number of control bits
bit_seq = []
for byte in msg_b: # get bytes to binary values; every bits store to sublist
bit_seq += list(map(int, f"{byte:08b}"))
res_len = ceil((len(msg_b) * 8) / mode) # length of result (bytes)
bit_seq += [0] * (res_len * mode - len(bit_seq)) # filling zeros
to_hamming = []
for i in range(res_len): # insert control bits into specified positions
code = bit_seq[i * mode:i * mode + mode]
for j in range(s_num):
code.insert(2 ** j - 1, 0)
to_hamming.append(code)
__hamming_common(to_hamming, s_num, True) # process
for i in to_hamming:
result += "".join(map(str, i))
return result
def hamming_decode(msg: str, mode: int=8) -> str:
"""
Decoding the message with Hamming code.
:param msg: Message string to decode
:param mode: number of significant bits
:return:
"""
result = ""
s_num = ceil(log2(log2(mode + 1) + mode + 1)) # number of control bits
res_len = len(msg) // (mode + s_num) # length of result (bytes)
code_len = mode + s_num # length of one code sequence
to_hamming = []
for i in range(res_len): # convert binary-like string to int-list
code = list(map(int, msg[i * code_len:i * code_len + code_len]))
to_hamming.append(code)
__hamming_common(to_hamming, s_num, False) # process
for i in to_hamming: # delete control bits
for j in range(s_num):
i.pop(2 ** j - 1 - j)
result += "".join(map(str, i))
msg_l = []
for i in range(len(result) // 8): # convert from binary-sring value to integer
val = "".join(result[i * 8:i * 8 + 8])
msg_l.append(int(val, 2))
result = bytes(msg_l).decode("utf-8") # finally decode to a regular string
return result
def noizer(msg: str, mode: int) -> str:
"""
Generates an error in each element of a Hamming encoded message
"""
seq = list(map(int, msg))
s_num = ceil(log2(log2(mode + 1) + mode + 1))
code_len = mode + s_num
cnt = len(msg) // code_len
result = ""
for i in range(cnt):
to_noize = seq[i * code_len:i * code_len + code_len]
noize = randrange(code_len)
to_noize[noize] = int(not to_noize[noize])
result += "".join(map(str, to_noize))
return result
if __name__ == "__main__":
MODE = 8 # count of signifed bits
### Menù ###
print("************************")
print("* *")
print("* Codifica Hamming *")
print("* *")
print("************************")
print("1) Codifica\n2) Decodifica\n3) Esci")
menu = int(input("Inserisci l'opzione scelta: "))
if menu == 1:
msg = input("Frase da codificare: ")
enc_msg = hamming_encode(msg,MODE) # Encode your message to Hamming code
print("enc:\n" + enc_msg)
noize_msg = noizer(enc_msg,MODE) # Noizer for encoded message
print("\nnoized:\n" + noize_msg)
dec_msg = hamming_decode(noize_msg,MODE) # Hamming decode
print("\ndec:\n" + dec_msg)
if menu == 2:
noize_msg = input("Codice da decodificare: ")
dec_msg = hamming_decode(noize_msg,MODE) # Hamming decode
print("\ndec:\n" + dec_msg)
if menu == 3:
exit()
elif menu > 3 or menu < 1:
print("\nScelta non valida.\nUscita...\n")
input("\nPremi INVIO per continuare...")