-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcrc.java
129 lines (97 loc) · 2.2 KB
/
crc.java
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import java.util.*;
class crc
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int k,g;
System.out.println("Enter degree of data :");
k=s.nextInt();
System.out.println("Enter degree of generator :");
g=s.nextInt();
int data[] = new int [200];
int gen[] = new int [200];
//function to input
System.out.println("Enter data: ");//113659.20
data=input(data,k);
System.out.println("Enter generator: ");
gen=input(gen,g);
//Pad the input with zeros
int codeword[] = new int[200];
for(int i=k;i<k+g-1;i++)
data[i]=0;
/*Disp after padding
System.out.println("Padded Codeword");
for(int i=0;i<k+g-1;i++)
System.out.println(codeword[i]);
*/
//Division part
//codeword=data because data was padded with zeros and stored in codeword
codeword=div(data,gen,k,g);
System.out.println("Checksum is :");
for(int i=k;i<k+g-1;i++)
{
data[i]=codeword[i];
System.out.println(data[i]);
}
System.out.println("Codeword is :");
for(int i=0;i<k+g-1;i++)
{
System.out.println(data[i]);
}
int codewordR[]=new int[200];
int dataR[]=new int[200];
System.out.println("RE-ENTER CODEWORD for reciever :");
//get user input for codewordR
dataR=input(codewordR,k+g-1);
//divide new codeword with gen
codewordR=div(dataR,gen,k,g);
int flag=0;
//System.out.println("After Division");
for(int i=k;i<k+g-1;i++)
{ if(codewordR[i]!=0)
{ flag=1;
break;
}
else
flag=0;
//System.out.println(codewordR[i]);
}
if(flag==1)
System.out.println("ERROR");
else
System.out.println("NO ERROR");
}
//Input helper function
public static int[] input(int arr[],int l)
{ Scanner s =new Scanner(System.in);
for(int i=0;i<l;i++)
arr[i]=s.nextInt();
return arr;
}
//Division Helper function,function recieves parameter k as n.
public static int[] div(int data[],int gen[],int n,int g)
{
int k,msb;
int r[]=new int[200];
for(int i=0;i<g;i++)
{
r[i]=data[i];
}
for(int i=0;i<n;i++)
{
k=0;
msb=r[i];
for(int j=i;j<g+i;j++)
{
if(msb==0)
r[j]=r[j]^0;
else
r[j]=r[j]^gen[k];
k++;
}
r[g+i]=data[g+i];
}
return r;
}
}