- Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcrc.java
More file actions
Latest commit
129 lines (97 loc) · 2.2 KB
/
Copy pathcrc.java
File metadata and controls
129 lines (97 loc) · 2.2 KB
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
importjava.util.*;
classcrc
{
publicstaticvoidmain(Stringargs[])
{
Scanners=newScanner(System.in);
intk,g;
System.out.println("Enter degree of data :");
k=s.nextInt();
System.out.println("Enter degree of generator :");
g=s.nextInt();
intdata[] = newint [200];
intgen[] = newint [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
intcodeword[] = newint[200];
for(inti=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(inti=k;i<k+g-1;i++)
{
data[i]=codeword[i];
System.out.println(data[i]);
}
System.out.println("Codeword is :");
for(inti=0;i<k+g-1;i++)
{
System.out.println(data[i]);
}
intcodewordR[]=newint[200];
intdataR[]=newint[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);
intflag=0;
//System.out.println("After Division");
for(inti=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
publicstaticint[] input(intarr[],intl)
{ Scanners =newScanner(System.in);
for(inti=0;i<l;i++)
arr[i]=s.nextInt();
returnarr;
}
//Division Helper function,function recieves parameter k as n.
publicstaticint[] div(intdata[],intgen[],intn,intg)
{
intk,msb;
intr[]=newint[200];
for(inti=0;i<g;i++)
{
r[i]=data[i];
}
for(inti=0;i<n;i++)
{
k=0;
msb=r[i];
for(intj=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];
}
returnr;
}
}