-
Notifications
You must be signed in to change notification settings - Fork 2
/
CheckSum.java
228 lines (217 loc) · 6.91 KB
/
CheckSum.java
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/**
* 6. Write a computer program that encode a dataword into a codeword and decode a codeword to determine whether the codeword is in error for checksum.
* Created by Peerachai Banyongrakkul Sec.1 5988070
* CheckSum.java
*/
import java.util.Scanner;
public class CheckSum
{
public static void main(String[] args)
{
System.out.println("---------- Checksum ----------");
System.out.println("ENCODE");
Scanner sc = new Scanner(System.in);
System.out.println("Dataword: ");
System.out.print("Please enter length of each segment: ");
int length = sc.nextInt();
String temp = sc.nextLine();
System.out.print("Segment 1: ");
String seg1 = sc.nextLine();
if(seg1.length() != length)
{
System.out.println("Wrong length!! Please try again");
System.exit(0);
}
System.out.print("Segment 2: ");
String seg2 = sc.nextLine();
if(seg1.length() != length)
{
System.out.println("Wrong length!! Please try again");
System.out.println("Both Segments have to have the same legnth.");
System.exit(0);
}
DataWord segment1 = new DataWord(seg1);
DataWord segment2 = new DataWord(seg2);
String checkSum = encode(segment1,segment2);
System.out.println("Codeword: " + segment1.getData() + " " + segment2.getData() + " " + checkSum);
System.out.println();
System.out.println("DECODE");
System.out.print("Received Codeword (input received checksum): " + segment1.getData() + " " +segment2.getData() + " ");
String ReCheckSum = sc.nextLine();
decode(segment1,segment2,ReCheckSum);
}
public static String encode(DataWord seg1,DataWord seg2)
{
int summation,sumwrap;
int s[] = new int[1000];
int w[] = new int[1000];
String sum = "";
String wrap = "";
String checkSum = "";
String temp1 = "";
String temp2 = "";
int segment1 = Integer.parseInt(seg1.getData(), 2);
int segment2 = Integer.parseInt(seg2.getData(), 2);
summation = segment1+segment2;
int i = 0;
while (summation > 0)
{
s[i] = summation%2;
summation = summation/2;
i++;
}
for(int j = i-1 ; j >= 0 ; j--)
{
sum += Integer.toString(s[j]);
}
System.out.println("Sum: " + sum);
if(sum.length() > seg1.getLength())
{
for(int j = 0 ; j < sum.length()-seg1.getLength() ; j++)
{
temp1 += sum.charAt(j);
}
for(int k = sum.length()-seg1.getLength() ; k < sum.length() ; k++)
{
temp2 += sum.charAt(k);
}
int warp1 = Integer.parseInt(temp1,2);
int warp2 = Integer.parseInt(temp2,2);
sumwrap = warp1+warp2;
//System.out.println(sumwrap);
int h = 0;
while (sumwrap > 0)
{
w[h] = sumwrap%2;
sumwrap = sumwrap/2;
h++;
}
for(int x = h-1 ; x >= 0 ; x--)
{
wrap += Integer.toString(w[x]);
}
System.out.println("Wrapped sum: " + wrap);
for(int y = 0 ; y < wrap.length() ; y++)
{
if(wrap.charAt(y) == '0')
{
checkSum += '1';
}
else
{
checkSum += '0';
}
}
}
else
{
for(int z = 0 ; z < seg1.getLength() ; z++)
{
if(sum.charAt(z) == '0')
{
checkSum += '1';
}
else
{
checkSum += '0';
}
}
}
System.out.println("checkSum: " + checkSum);
return checkSum;
}
public static void decode(DataWord seg1, DataWord seg2, String cs)
{
int summation,sumwrap;
int s[] = new int[1000];
int w[] = new int[1000];
String sum = "";
String wrap = "";
String temp1 = "";
String temp2 = "";
String checkSum = "";
int segment1 = Integer.parseInt(seg1.getData(), 2);
int segment2 = Integer.parseInt(seg2.getData(), 2);
int checkS = Integer.parseInt(cs,2);
summation = segment1+segment2+checkS;
int i = 0;
while (summation > 0)
{
s[i] = summation%2;
summation = summation/2;
i++;
}
for(int j = i-1 ; j >= 0 ; j--)
{
sum += Integer.toString(s[j]);
}
System.out.println("Segment1: " + seg1.getData());
System.out.println("Segment2: " + seg2.getData());
System.out.println("Received Checksum: " + cs);
System.out.println("Sum: " + sum);
if(sum.length() > seg1.getLength())
{
for(int j = 0 ; j < sum.length()-seg1.getLength() ; j++)
{
temp1 += sum.charAt(j);
}
for(int k = sum.length()-seg1.getLength() ; k < sum.length() ; k++)
{
temp2 += sum.charAt(k);
}
int warp1 = Integer.parseInt(temp1,2);
int warp2 = Integer.parseInt(temp2,2);
sumwrap = warp1+warp2;
//System.out.println(sumwrap);
int h = 0;
while (sumwrap > 0)
{
w[h] = sumwrap%2;
sumwrap = sumwrap/2;
h++;
}
for(int x = h-1 ; x >= 0 ; x--)
{
wrap += Integer.toString(w[x]);
}
System.out.println("Wrapped sum: " + wrap);
for(int y = 0 ; y < wrap.length() ; y++)
{
if(wrap.charAt(y) == '0')
{
checkSum += '1';
}
else
{
checkSum += '0';
}
}
}
else
{
for(int z = 0 ; z < seg1.getLength() ; z++)
{
if(sum.charAt(z) == '0')
{
checkSum += '1';
}
else
{
checkSum += '0';
}
}
}
System.out.println("checkSum: " + checkSum);
System.out.println();
if(Integer.parseInt(checkSum) == 0)
{
System.out.println("Syndrome: 0");
System.out.println("***The codeword isn't in error");
}
else
{
System.out.println("Syndrome: non-zero");
System.out.println("***The codeword is in error");
}
}
}