-
Notifications
You must be signed in to change notification settings - Fork 2
/
crc.cpp
49 lines (40 loc) · 1.05 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
#include<iostream>
#include<string.h>
using namespace std;
string crc(string data, string poly, bool errChk){
string rem = data;
if (!errChk){
for(int i=0; i<poly.length()-1;i++)
rem.append("0");
}
for(int i=0;i<rem.length()-poly.length()+1;i++){
if (rem[i]=='1'){
for(int j=0; j<poly.length();j++){
rem[i+j] = (rem[i+j]==poly[j]) ? '0': '1';
}
}
}
return rem.substr(rem.length()-poly.length()+1);
}
int main(){
string data;
string poly = "10000100010001010";
cout << "Enter Data to be sent: " ;
cin >> data;
string rem = crc(data,poly,0);
string codeword = data+rem;
cout << "Remainder: " << rem << endl;
cout << "Codeword: " << codeword << endl;
//Checking error
string recvCodeword;
cout << "Enter received codeword: " ;
cin >> recvCodeword;
string recvRem = crc(recvCodeword, poly, 1);
if (stoi(recvRem)==0){
cout << "No Error";
}
else{
cout << "Error Detected";
}
return 0;
}