|
| 1 | +import java.util.*; |
| 2 | + |
| 3 | +class crc |
| 4 | +{ |
| 5 | + |
| 6 | + |
| 7 | + public static void main(String args[]) |
| 8 | + { |
| 9 | + Scanner s=new Scanner(System.in); |
| 10 | + |
| 11 | + int k,g; |
| 12 | + System.out.println("Enter degree of data :"); |
| 13 | + k=s.nextInt(); |
| 14 | + System.out.println("Enter degree of generator :"); |
| 15 | + g=s.nextInt(); |
| 16 | + |
| 17 | + int data[] = new int [200]; |
| 18 | + int gen[] = new int [200]; |
| 19 | + |
| 20 | + //function to input |
| 21 | + System.out.println("Enter data: ");//113659.20 |
| 22 | + data=input(data,k); |
| 23 | + System.out.println("Enter generator: "); |
| 24 | + gen=input(gen,g); |
| 25 | + |
| 26 | + //Pad the input with zeros |
| 27 | + int codeword[] = new int[200]; |
| 28 | + |
| 29 | + for(int i=k;i<k+g-1;i++) |
| 30 | + data[i]=0; |
| 31 | + |
| 32 | + /*Disp after padding |
| 33 | + System.out.println("Padded Codeword"); |
| 34 | + for(int i=0;i<k+g-1;i++) |
| 35 | + System.out.println(codeword[i]); |
| 36 | + */ |
| 37 | + //Division part |
| 38 | + //data=codeword bcause data was padded with zeros and stored in codeword |
| 39 | + codeword=div(data,gen,k,g); |
| 40 | + System.out.println("Checksum is :"); |
| 41 | + for(int i=k;i<k+g-1;i++) |
| 42 | + { |
| 43 | + data[i]=codeword[i]; |
| 44 | + System.out.println(data[i]); |
| 45 | + } |
| 46 | + |
| 47 | + System.out.println("Codeword is :"); |
| 48 | + for(int i=0;i<k+g-1;i++) |
| 49 | + { |
| 50 | + System.out.println(data[i]); |
| 51 | + } |
| 52 | + |
| 53 | + int codewordR[]=new int[200]; |
| 54 | + int dataR[]=new int[200]; |
| 55 | + System.out.println("RE-ENTER CODEWORD for reciever :"); |
| 56 | + //get user input for codewordR |
| 57 | + dataR=input(codewordR,k+g-1); |
| 58 | + |
| 59 | + //divide new codeword with gen |
| 60 | + codewordR=div(dataR,gen,k,g); |
| 61 | + int flag=0; |
| 62 | + //System.out.println("After Division"); |
| 63 | + for(int i=k;i<k+g-1;i++) |
| 64 | + { if(codewordR[i]!=0) |
| 65 | + { flag=1; |
| 66 | + |
| 67 | + } |
| 68 | + else |
| 69 | + flag=0; |
| 70 | + //System.out.println(codewordR[i]); |
| 71 | + } |
| 72 | + if(flag==1) |
| 73 | + System.out.println("ERROR"); |
| 74 | + |
| 75 | + else |
| 76 | + System.out.println("NO ERROR"); |
| 77 | + } |
| 78 | + |
| 79 | + //Input helper function |
| 80 | + public static int[] input(int arr[],int l) |
| 81 | + { Scanner s =new Scanner(System.in); |
| 82 | + for(int i=0;i<l;i++) |
| 83 | + arr[i]=s.nextInt(); |
| 84 | + return arr; |
| 85 | + } |
| 86 | + |
| 87 | + //Division Helper function,function recieves parameter k as n. |
| 88 | + public static int[] div(int data[],int gen[],int n,int g) |
| 89 | + { |
| 90 | + int k,msb; |
| 91 | + int r[]=new int[200]; |
| 92 | + |
| 93 | + for(int i=0;i<g;i++) |
| 94 | + { |
| 95 | + r[i]=data[i]; |
| 96 | + } |
| 97 | + |
| 98 | + for(int i=0;i<n;i++) |
| 99 | + { |
| 100 | + k=0; |
| 101 | + |
| 102 | + msb=r[i]; |
| 103 | + for(int j=i;j<g+i;j++) |
| 104 | + { |
| 105 | + if(msb==0) |
| 106 | + r[j]=r[j]^0; |
| 107 | + else |
| 108 | + r[j]=r[j]^gen[k]; |
| 109 | + |
| 110 | + k++; |
| 111 | + |
| 112 | + } |
| 113 | + |
| 114 | + |
| 115 | + r[g+i]=data[g+i]; |
| 116 | + } |
| 117 | + |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | + return r; |
| 122 | + |
| 123 | + |
| 124 | + } |
| 125 | + |
| 126 | + |
| 127 | + |
| 128 | +} |
0 commit comments