|
| 1 | +# Cyclic Redundancy Check |
| 2 | + |
| 3 | +CRC or Cyclic Redundancy Check is a method of detecting accidental changes/errors in the communication channel. |
| 4 | + |
| 5 | +```java |
| 6 | + |
| 7 | +import java.util.Arrays; |
| 8 | +class Program { |
| 9 | + |
| 10 | + |
| 11 | + static String Xor(String a, String b) |
| 12 | + { |
| 13 | + |
| 14 | + |
| 15 | + String result = ""; |
| 16 | + int n = b.length(); |
| 17 | + |
| 18 | + for (int i = 1; i < n; i++) { |
| 19 | + if (a.charAt(i) == b.charAt(i)) |
| 20 | + result += "0"; |
| 21 | + else |
| 22 | + result += "1"; |
| 23 | + } |
| 24 | + return result; |
| 25 | + } |
| 26 | + static String Mod2Div(String dividend, String divisor) |
| 27 | + { |
| 28 | + |
| 29 | + int pick = divisor.length(); |
| 30 | + |
| 31 | + |
| 32 | + String tmp = dividend.substring(0, pick); |
| 33 | + |
| 34 | + int n = dividend.length(); |
| 35 | + |
| 36 | + while (pick < n) { |
| 37 | + if (tmp.charAt(0) == '1') |
| 38 | + |
| 39 | + tmp = Xor(divisor, tmp) |
| 40 | + + dividend.charAt(pick); |
| 41 | + else |
| 42 | + |
| 43 | + |
| 44 | + tmp = Xor(new String(new char[pick]) |
| 45 | + .replace("\0", "0"), |
| 46 | + tmp) |
| 47 | + + dividend.charAt(pick); |
| 48 | + |
| 49 | + |
| 50 | + pick += 1; |
| 51 | + } |
| 52 | + |
| 53 | + |
| 54 | + if (tmp.charAt(0) == '1') |
| 55 | + tmp = Xor(divisor, tmp); |
| 56 | + else |
| 57 | + tmp = Xor(new String(new char[pick]) |
| 58 | + .replace("\0", "0"), |
| 59 | + tmp); |
| 60 | + |
| 61 | + return tmp; |
| 62 | + } |
| 63 | + |
| 64 | + |
| 65 | + static void EncodeData(String data, String key) |
| 66 | + { |
| 67 | + int l_key = key.length(); |
| 68 | + |
| 69 | + String appended_data |
| 70 | + = (data |
| 71 | + + new String(new char[l_key - 1]) |
| 72 | + .replace("\0", "0")); |
| 73 | + |
| 74 | + String remainder = Mod2Div(appended_data, key); |
| 75 | + |
| 76 | + |
| 77 | + String codeword = data + remainder; |
| 78 | + System.out.println("Remainder : " + remainder); |
| 79 | + System.out.println( |
| 80 | + "Encoded Data (Data + Remainder) :" + codeword |
| 81 | + + "\n"); |
| 82 | + } |
| 83 | + static void Receiver(String data, String key) |
| 84 | + { |
| 85 | + String currxor |
| 86 | + = Mod2Div(data.substring(0, key.length()), key); |
| 87 | + int curr = key.length(); |
| 88 | + while (curr != data.length()) { |
| 89 | + if (currxor.length() != key.length()) { |
| 90 | + currxor += data.charAt(curr++); |
| 91 | + } |
| 92 | + else { |
| 93 | + currxor = Mod2Div(currxor, key); |
| 94 | + } |
| 95 | + } |
| 96 | + if (currxor.length() == key.length()) { |
| 97 | + currxor = Mod2Div(currxor, key); |
| 98 | + } |
| 99 | + if (currxor.contains("1")) { |
| 100 | + System.out.println( |
| 101 | + "there is some error in data"); |
| 102 | + } |
| 103 | + else { |
| 104 | + System.out.println("correct message received"); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + public static void main(String[] args) |
| 109 | + { |
| 110 | + String data = "100100"; |
| 111 | + String key = "1101"; |
| 112 | + System.out.println("\nSender side..."); |
| 113 | + EncodeData(data, key); |
| 114 | + |
| 115 | + System.out.println("Receiver side..."); |
| 116 | + Receiver(data+Mod2Div(data+new String(new char[key.length() - 1]) |
| 117 | + .replace("\0", "0"),key),key); |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +``` |
0 commit comments