-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCRC.cpp
68 lines (58 loc) · 1.38 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
#include<bits/stdc++.h>
using namespace std;
string xor1(string a,string b){
string ans;
for(int i=1;i<b.length();i++){
if(a[i]==b[i])
ans += "0";
else
ans += "1";
}
return ans;
}
string solve(string &appdata,string &key){
string quotient;
string temp;
string temp2;
int len = key.length();
for(int i=0;i<len;i++){
temp += appdata[i];
temp2 += '0';
}
while(len<appdata.length()){
if(temp[0]=='1'){
quotient += '1';
temp = xor1(temp,key)+appdata[len];
}else{
quotient += '0';
temp = xor1(temp2,temp)+appdata[len];
}
len++;
}
if(temp[0]=='1'){
quotient += '1';
temp = xor1(temp,key);
}else{
quotient += '0';
temp = xor1(temp2,temp);
}
cout<<"quotient is: "<<quotient<<endl;
return temp;
}
void CRC(string &data,string &key){
string appdata=data;
for(int i=0;i<key.length()-1;i++)
appdata += '0';
string rem = solve(appdata,key);
cout<<"Remainder is : "<<rem<<endl;
string ans = data + rem;
cout<<"Codeword is : "<<ans<<endl;
}
int main() {
cout<<"Enter the data: "<<endl;
string data; cin>>data;
cout<<"Enter the key: "<<endl;
string key; cin>>key;
CRC(data,key);
return 0;
}