Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.51 KB | None | 0 0
  1. /*
  2. * Kevin Garcia
  3. * CIS 3360 CRC PROGRAM
  4. * Dr. Yuechun Chu
  5. */
  6.  
  7. package crcprototype;
  8.  
  9. import java.io.*;
  10.  
  11. class Checksum {
  12.  
  13. /*
  14.  
  15. userHex is the message entered by user.
  16.  
  17. divisor is the polynomial entered which will be used for Xoring
  18.  
  19. crc is the CRC(Checksum) calculated
  20.  
  21. tempDVD is used to generate the Initial padded message which can be used to calculate CRC
  22.  
  23. dvdLen is the Length of Dividend
  24.  
  25. divLen is the length of the divisor
  26.  
  27. */
  28. String userHex, crc, tempDVD;
  29.  
  30. private static String divisor = "1100110110101";
  31.  
  32. int dvdLen, divLen, i;
  33.  
  34. int initialLength;
  35.  
  36. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  37.  
  38. int padding = 0;//This holds the number of red zeros to be added. 0 represents the red zeros
  39.  
  40. void printDivisor(String data) {
  41.  
  42. //Print the divisor
  43. if (data.length() >= divLen) {
  44.  
  45. System.out.print("\t\t\t");
  46.  
  47. for (i = 0; i < padding - 1; i++) {
  48.  
  49. System.out.print(" ");
  50.  
  51. }
  52.  
  53. System.out.println("0" + divisor);
  54.  
  55. }
  56.  
  57. }
  58.  
  59. void printXorResult(String data) {
  60.  
  61. //Print XOR result in the specified format
  62.  
  63. System.out.print("result is \t\t0");
  64.  
  65. for (i = 0; i < padding; i++) {
  66.  
  67. System.out.print("0");
  68.  
  69. }
  70.  
  71. if (data.length() > divLen) {
  72.  
  73. System.out.print(data.substring(0, divLen));
  74.  
  75. } else {
  76.  
  77. System.out.print(data);
  78.  
  79. }
  80.  
  81. for (i = (padding + divLen); i < initialLength; i++) {
  82.  
  83. System.out.print("0");
  84.  
  85. }
  86.  
  87. System.out.println();
  88.  
  89. }
  90.  
  91. void calculateCRC(String data) {
  92.  
  93. String procuserHex;//The string to be used for Xoring
  94.  
  95. int temp;
  96.  
  97. dvdLen = data.length();
  98.  
  99. initialLength = dvdLen;//To keep a record of initial length of dividend since dvdLen changes again and again
  100.  
  101. while (dvdLen >= divLen) {
  102.  
  103. procuserHex = data.substring(0, divLen);//Get the bits to be used for Xoring
  104.  
  105. //Perform Xoring by converting them into integers
  106. temp = Integer.parseInt(procuserHex, 2) ^ Integer.parseInt(divisor, 2);
  107.  
  108. padding += divLen - Integer.toString(temp, 2).length();//Check the number of Red 0's to be padded(*0 in this case)
  109.  
  110. if (temp != 0) {
  111.  
  112. data = Integer.toString(temp, 2) + data.substring(divLen, data.length());
  113.  
  114. //Bring down more bits if necessary
  115. } else {
  116.  
  117. data = data.substring(divLen, data.length());
  118.  
  119. //The remainder is 0 so get more bits from dividend
  120. }
  121.  
  122. printXorResult(data);
  123.  
  124. printDivisor(data);
  125.  
  126. //Integer.toString(temp,2) or Integer.toBinaryString() will do the same thing
  127. dvdLen = data.length();
  128.  
  129. }
  130.  
  131. //Append 0s to the Checksum to generate a valid checksum which is same in length as the divisor
  132. if (data.length() < (divLen - 1)) {
  133.  
  134. while (data.length() != (divLen - 1)) {
  135.  
  136. data = "0" + data;
  137.  
  138. }
  139.  
  140. }
  141.  
  142. crc = data;
  143. System.out.println(">>> CRC is : 0000000000000" + data + "("
  144. + Integer.toHexString(Integer.parseInt(crc, 2)).toLowerCase()
  145. + ")");
  146.  
  147. }
  148.  
  149. void sender() throws Exception {
  150. System.out.println("=====CALCULATE CRC======");
  151. System.out.print("Enter Hexadecimal: ");
  152.  
  153. userHex = br.readLine();
  154.  
  155. System.out.print("\nMsg is: " + userHex);
  156.  
  157. //Convert the input hex to binary
  158. String hexPart = userHex.substring(2);//Get the Hex part since the input is padded with 0x
  159.  
  160. userHex = Integer.toBinaryString(Integer.parseInt(hexPart, 16));
  161.  
  162. //userHex = "0" + userHex;
  163. //System.out.print("Enter Divisor ");
  164. //divisor = br.readLine();
  165. System.out.print(" (" + userHex + ")\n");
  166. System.out.println(">>>>>>");
  167.  
  168. divLen = divisor.length();
  169.  
  170. i = 0;
  171.  
  172. tempDVD = userHex;
  173.  
  174. //Generate the padded message
  175. while (i < (divLen - 1)) {
  176.  
  177. tempDVD = tempDVD.concat("0");
  178. i++;
  179.  
  180. }
  181.  
  182. System.out.println("padmsg is 0" + tempDVD);
  183.  
  184. System.out.println("polnom is 0" + divisor + "0000000000");//X Represents XOR Operation
  185.  
  186. dvdLen = tempDVD.length();
  187.  
  188. calculateCRC(tempDVD);
  189.  
  190. System.out.println("Final result is " + Integer.toHexString(Integer.parseInt(userHex + crc, 2)).toLowerCase());
  191.  
  192. }
  193.  
  194. void receiver() throws Exception {
  195.  
  196. padding = 0;
  197.  
  198. System.out.println("\n=====VERIFY CRC======");
  199.  
  200. System.out.print("Enter the Message Received: ");
  201.  
  202. String recData = br.readLine();
  203.  
  204. String hexPart = recData.substring(2);//Get the Hex part since the input is padded with 0x
  205.  
  206. recData = Integer.toBinaryString(Integer.parseInt(hexPart, 16));
  207.  
  208. System.out.println("padmsg is 0" + tempDVD);
  209.  
  210. //System.out.println("Received Data Is " + hexPart);
  211. calculateCRC(recData);
  212.  
  213. if (Integer.parseInt(crc) == 0)//If the CRC is 0 then there is no error
  214. {
  215.  
  216. System.out.println("Message has been received Without Error");
  217.  
  218. } else {
  219.  
  220. System.out.println("Message has been received With Error");
  221.  
  222. }
  223.  
  224. }
  225.  
  226. }
  227.  
  228. public class CRCPrototype {
  229.  
  230. public static void main(String args[]) throws Exception {
  231.  
  232. Checksum cc = new Checksum();
  233.  
  234. cc.sender();
  235.  
  236. cc.receiver();
  237.  
  238. }
  239.  
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement