Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Kevin Garcia
- * CIS 3360 CRC PROGRAM
- * Dr. Yuechun Chu
- */
- package crcprototype;
- import java.io.*;
- class Checksum {
- /*
- userHex is the message entered by user.
- divisor is the polynomial entered which will be used for Xoring
- crc is the CRC(Checksum) calculated
- tempDVD is used to generate the Initial padded message which can be used to calculate CRC
- dvdLen is the Length of Dividend
- divLen is the length of the divisor
- */
- String userHex, crc, tempDVD;
- private static String divisor = "1100110110101";
- int dvdLen, divLen, i;
- int initialLength;
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- int padding = 0;//This holds the number of red zeros to be added. 0 represents the red zeros
- void printDivisor(String data) {
- //Print the divisor
- if (data.length() >= divLen) {
- System.out.print("\t\t\t");
- for (i = 0; i < padding - 1; i++) {
- System.out.print(" ");
- }
- System.out.println("0" + divisor);
- }
- }
- void printXorResult(String data) {
- //Print XOR result in the specified format
- System.out.print("result is \t\t0");
- for (i = 0; i < padding; i++) {
- System.out.print("0");
- }
- if (data.length() > divLen) {
- System.out.print(data.substring(0, divLen));
- } else {
- System.out.print(data);
- }
- for (i = (padding + divLen); i < initialLength; i++) {
- System.out.print("0");
- }
- System.out.println();
- }
- void calculateCRC(String data) {
- String procuserHex;//The string to be used for Xoring
- int temp;
- dvdLen = data.length();
- initialLength = dvdLen;//To keep a record of initial length of dividend since dvdLen changes again and again
- while (dvdLen >= divLen) {
- procuserHex = data.substring(0, divLen);//Get the bits to be used for Xoring
- //Perform Xoring by converting them into integers
- temp = Integer.parseInt(procuserHex, 2) ^ Integer.parseInt(divisor, 2);
- padding += divLen - Integer.toString(temp, 2).length();//Check the number of Red 0's to be padded(*0 in this case)
- if (temp != 0) {
- data = Integer.toString(temp, 2) + data.substring(divLen, data.length());
- //Bring down more bits if necessary
- } else {
- data = data.substring(divLen, data.length());
- //The remainder is 0 so get more bits from dividend
- }
- printXorResult(data);
- printDivisor(data);
- //Integer.toString(temp,2) or Integer.toBinaryString() will do the same thing
- dvdLen = data.length();
- }
- //Append 0s to the Checksum to generate a valid checksum which is same in length as the divisor
- if (data.length() < (divLen - 1)) {
- while (data.length() != (divLen - 1)) {
- data = "0" + data;
- }
- }
- crc = data;
- System.out.println(">>> CRC is : 0000000000000" + data + "("
- + Integer.toHexString(Integer.parseInt(crc, 2)).toLowerCase()
- + ")");
- }
- void sender() throws Exception {
- System.out.println("=====CALCULATE CRC======");
- System.out.print("Enter Hexadecimal: ");
- userHex = br.readLine();
- System.out.print("\nMsg is: " + userHex);
- //Convert the input hex to binary
- String hexPart = userHex.substring(2);//Get the Hex part since the input is padded with 0x
- userHex = Integer.toBinaryString(Integer.parseInt(hexPart, 16));
- //userHex = "0" + userHex;
- //System.out.print("Enter Divisor ");
- //divisor = br.readLine();
- System.out.print(" (" + userHex + ")\n");
- System.out.println(">>>>>>");
- divLen = divisor.length();
- i = 0;
- tempDVD = userHex;
- //Generate the padded message
- while (i < (divLen - 1)) {
- tempDVD = tempDVD.concat("0");
- i++;
- }
- System.out.println("padmsg is 0" + tempDVD);
- System.out.println("polnom is 0" + divisor + "0000000000");//X Represents XOR Operation
- dvdLen = tempDVD.length();
- calculateCRC(tempDVD);
- System.out.println("Final result is " + Integer.toHexString(Integer.parseInt(userHex + crc, 2)).toLowerCase());
- }
- void receiver() throws Exception {
- padding = 0;
- System.out.println("\n=====VERIFY CRC======");
- System.out.print("Enter the Message Received: ");
- String recData = br.readLine();
- String hexPart = recData.substring(2);//Get the Hex part since the input is padded with 0x
- recData = Integer.toBinaryString(Integer.parseInt(hexPart, 16));
- System.out.println("padmsg is 0" + tempDVD);
- //System.out.println("Received Data Is " + hexPart);
- calculateCRC(recData);
- if (Integer.parseInt(crc) == 0)//If the CRC is 0 then there is no error
- {
- System.out.println("Message has been received Without Error");
- } else {
- System.out.println("Message has been received With Error");
- }
- }
- }
- public class CRCPrototype {
- public static void main(String args[]) throws Exception {
- Checksum cc = new Checksum();
- cc.sender();
- cc.receiver();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement