-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathChecksum.java
More file actions
158 lines (122 loc) · 3.74 KB
/
Copy pathChecksum.java
File metadata and controls
158 lines (122 loc) · 3.74 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
import java.util.*;
public class Checksum
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("(Sender)Enter data:");
String input = sc.next();
int checksum = generate(input);
System.out.println("Checksum generated = " + Integer.toHexString(checksum));
System.out.println("\n(Receiver)Enter data:");
input = sc.next();
System.out.println("\nEnter the checksum generated in the sender's side:");
checksum = Integer.parseInt((sc.next()), 16);
receive(input, checksum);
}
private static int generate(String s)
{
String hex_value = new String();
int x, i, checksum = 0;
for (i = 0; i < s.length() - 2; i = i + 2)
{
x = (int) (s.charAt(i));
hex_value = Integer.toHexString(x);
x = (int) (s.charAt(i + 1));
hex_value = hex_value + Integer.toHexString(x);
System.out.println(s.charAt(i) + "" + s.charAt(i + 1) + " : "
+ hex_value);
x = Integer.parseInt(hex_value, 16);
checksum += x;
}
if (s.length() % 2 == 0)
{
x = (int) (s.charAt(i));
hex_value = Integer.toHexString(x);
x = (int) (s.charAt(i + 1));
hex_value = hex_value + Integer.toHexString(x);
System.out.println(s.charAt(i) + "" + s.charAt(i + 1) + " : "
+ hex_value);
x = Integer.parseInt(hex_value, 16);
}
else
{
x = (int) (s.charAt(i));
hex_value = "00" + Integer.toHexString(x);
x = Integer.parseInt(hex_value, 16);
System.out.println(s.charAt(i) + " : " + hex_value);
}
checksum += x;
hex_value = Integer.toHexString(checksum);
if (hex_value.length() > 4)
{
int carry = Integer.parseInt(("" + hex_value.charAt(0)), 16);
hex_value = hex_value.substring(1, 5);
checksum = Integer.parseInt(hex_value, 16);
checksum += carry;
}
checksum = generateComplement(checksum);
return checksum;
}
static void receive(String s, int checksum)
{
int generated_checksum = generate(s);
generated_checksum = generateComplement(generated_checksum);
int syndrome = generated_checksum + checksum;
syndrome = generateComplement(syndrome);
System.out.println("Output= " + Integer.toHexString(syndrome));
if (syndrome == 0)
{
System.out.println("\nNo error");
}
else
{
System.out.println("\nError");
}
}
static int generateComplement(int checksum)
{
checksum = Integer.parseInt("FFFF", 16) - checksum;
return checksum;
}
}
/* OUTPUT-1
*
*(Sender)Enter data:
forouzan
fo : 666f
ro : 726f
uz : 757a
an : 616e
Checksum generated = 5038
(Receiver)Enter data:
forouzan
Enter the checksum generated in the sender's side:
5038
fo : 666f
ro : 726f
uz : 757a
an : 616e
Output= 0
No error
*/
/* OUTPUT-2
*
*(Sender)Enter data:
forouzan
fo : 666f
ro : 726f
uz : 757a
an : 616e
Checksum generated = 5038
(Receiver)Enter data:
kennedy
Enter the checksum generated in the sender's side:
5038
ke : 6b65
nn : 6e6e
ed : 6564
y : 0079
Output= 7016
Error
*/