-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hamming-Code.c
206 lines (154 loc) · 4.53 KB
/
Hamming-Code.c
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include <stdio.h>
#define max 100
int asciiToBinary(int input) {
int result = 0, i = 1, remainder;
/// convert ascii to binary format
while (input > 0) {
remainder = input % 2;
result = result + (i * remainder);
input = input / 2;
i = i * 10;
}
return(result);
}
int main() {
int i,r, binary[max], temp[max], dataRec[max],sizeofArr = 0, finalArray[max];
char input[max];
int finalArraySizeCounter=0;
/*FILE*fp;
fp = fopen("Drive path\txtFile.txt","r");
fgets( input, 100, fp );//line input
fclose (fp);*/
printf("Enter only 5 characters: ");
for( i = 0; i <5; i++){
scanf("%c",&input[i]);
}
for(i=0;input[i] != '\0';i++){
int ch = input[i];
binary[i]=asciiToBinary(ch);
}
for(i=0;input[i] != '\0';i++){
sizeofArr++;
}
for(int x = 0; x<sizeofArr;x++){
for(i=6; i>=0; i--){
r = binary[x]%10;
binary[x] = binary[x]/10;
temp[i] = r;
}
for(i=0;i<=6;i++){
finalArray[finalArraySizeCounter]=temp[i];
finalArraySizeCounter++;
}
}
///Finding redundent bit
int dataword[100],j=0;
int r1 = 0, r2 = 1,r4 = 3, r8 = 7,k; //redundent bit pos
int d3 = 2, d5 = 4,d6 = 5, d7 = 6,d9 = 8, d10 = 9,d11 = 10; //data bit pos
for(k =0; k < sizeofArr;k++){
for(i=r1; i<=(r1+10);i++){
if(i==r1 || i==r2 || i==r4 || i==r8){
dataword[i]=0;
}
else{
dataword[i]=finalArray[j];
j++;
}
}
dataword[r1] = dataword[d3]^dataword[d5]^dataword[d7]^dataword[d9]^dataword[d11];
dataword[r2] = dataword[d3]^dataword[d6]^dataword[d7]^dataword[d10]^dataword[d11];
dataword[r4] = dataword[d5]^dataword[d6]^dataword[d7];
dataword[r8] = dataword[d9]^dataword[d10]^dataword[d11];
r1+=11;
r2+=11;
r4+=11;
r8+=11;
d3+=11;
d5+=11;
d6+=11;
d7+=11;
d9+=11;
d10+=11;
d11+=11;
}
///Print Data words
printf("\nData words : \n");
for(i=0;i<finalArraySizeCounter;i++){
if(i%7==0){
printf(" ");
}
printf("%d ",finalArray[i]);
}
///Print Code Words
printf("\n\nCode words : \n");
for(i=0;i<=((10*sizeofArr)+(sizeofArr-1));i++){
if(i%11==0){
printf(" ");
}
printf("%d ",dataword[i]);
}
///Error detect
int errorPos = 0;
int p1=0, p2=0, p4=0, p8=0;
int bits;
int dx1 = 0,dx2 = 1,dx3 = 2,dx4 = 3,dx5 = 4,dx6 = 5,dx7 = 6;
int dx8 = 7,dx9 = 8,dx10 = 9,dx11 = 10;
printf("\n\nHow many bits you have recieved: ");
scanf("%d",&bits);
printf("\n\nWhat have you recieved enter bits one by one:\n");
for(i=0; i < bits; i++){
scanf("%d",&dataRec[i]);
}
for(k =0; k < sizeofArr;k++){
p1 = dataRec[dx1]^dataRec[dx3]^dataRec[dx5]^dataRec[dx7]^dataRec[dx9]^dataRec[dx11];
p2 = dataRec[dx2]^dataRec[dx3]^dataRec[dx6]^dataRec[dx7]^dataRec[dx10]^dataRec[dx11];
p4 = dataRec[dx4]^dataRec[dx5]^dataRec[dx6]^dataRec[dx7];
p8 = dataRec[dx8]^dataRec[dx9]^dataRec[dx10]^dataRec[dx11];
if(p1==1 || p2==1 || p4==1 || p8==1){
///Error detection & corrections
if(p1==1){
errorPos += 1;
}
if(p2==1){
errorPos += 2;
}
if(p4==1){
errorPos += 4;
}
if(p8==1){
errorPos += 8;
}
printf("\nError found at position %d",errorPos);
if (dataRec[errorPos-1]==1){
dataRec[errorPos-1]=0;
}
else{
dataRec[errorPos-1]=1;
}
errorPos=((k+1)*11);
printf("\n\nAfter Correction the bits are: \n");
for(i=0; i <= ((10*sizeofArr)+(sizeofArr-1)); i++){
if(i%11==0){
printf(" ");
}
printf("%d ",dataRec[i]);
}
}
else{
printf("\n\nNo Error\n");
errorPos+=11;
}
dx1+=11;
dx2+=11;
dx3+=11;
dx4+=11;
dx5+=11;
dx6+=11;
dx7+=11;
dx8+=11;
dx9+=11;
dx10+=11;
dx11+=11;
}
return 0;
}