-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleCRC.java
More file actions
30 lines (26 loc) · 905 Bytes
/
SimpleCRC.java
File metadata and controls
30 lines (26 loc) · 905 Bytes
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
public class SimpleCRC {
public static void main(String[] args) {
String data = "1101";
String gen = "101";
String crc = crc(data, gen);
System.out.println("CRC: " + crc);
System.out.println("Send: " + data + crc);
}
static String crc(String data, String gen) {
String d = data + "0".repeat(gen.length() - 1);
String t = d.substring(0, gen.length());
for (int i = gen.length(); i <= d.length(); i++) {
if (t.charAt(0) == '1') {
String r = "";
for (int j = 0; j < gen.length(); j++) {
r += t.charAt(j) == gen.charAt(j) ? '0' : '1';
}
t = r;
}
if (i < d.length()) {
t = t.substring(1) + d.charAt(i);
}
}
return t.substring(1);
}
}