-
Notifications
You must be signed in to change notification settings - Fork 0
/
CRC.cpp
72 lines (64 loc) · 1.1 KB
/
CRC.cpp
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
#include<bits/stdc++.h>
using namespace std;
void xor_op(string &c_rem,string d)
{
string temp = c_rem;
for(int i = 1;i<c_rem.length();i++)
{
if(c_rem[i] == d[i])
{
temp[i-1] = '0';
}
else
{
temp[i-1] = '1';
}
}
c_rem = temp;
}
int main()
{
string data;
cout<<"Enter Received Data block : ";
cin>>data;
string divisor;
cout<<"Enter divisor : ";
cin>>divisor;
int dn = divisor.length();
string d1 = divisor;
string d2, fin_rem = "";
d2.append(dn,'0');
string c_rem = data.substr(0,dn);
for(int i = 0;i<data.length() - dn;i++)
{
if(c_rem[i] == '1')
{
xor_op(c_rem,d1);
if(i + dn <= data.length() -1)
{
c_rem[dn-1] = data[i+dn];
//cout<<c_rem<<endl;
}
}
else
{
xor_op(c_rem,d2);
if(i + dn <= data.length() -1)
{
c_rem[dn-1] = data[i+dn];
//cout<<c_rem<<endl;
}
}
}
c_rem.pop_back();
cout<<"Remainder : "<<c_rem<<endl;
if(c_rem == fin_rem.append(dn-1,'0'))
{
cout<<"Correct Data"<<endl;
}
else
{
cout<<"Incorrect/Errored Data"<<endl;
}
return 0;
}