-
Notifications
You must be signed in to change notification settings - Fork 0
/
crc.cpp
63 lines (46 loc) · 1.33 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
#include <iostream>
using namespace std;
string xorfun(string encoded, string crc)
{
int crclen = crc.length();
for (int i = 0; i <= (encoded.length() - crclen);)
{
for (int j = 0; j < crclen; j++)
{
encoded[i + j] = encoded[i + j] == crc[j] ? '0': '1';
}
for (; i < encoded.length() && encoded[i] != '1'; i++);
}
return encoded;
}
int main() {
string data="";
string crc="";
string encoded = "";
cout << "Enter Data bits: " << endl;
cin >> data;
cout << "Enter Generator: " << endl;
cin>> crc;
encoded += data;
int datalen = data.length();
int crclen = crc.length();
for (int i = 1; i <= (crclen - 1); i++)
encoded += '0';
encoded = xorfun(encoded, crc);
cout << "Generated Codeword: ";
cout << encoded.substr(encoded.length() - crclen + 1) << endl;
cout << "Transmitted Message: ";
cout << data + encoded.substr(encoded.length() - crclen +1)<<endl;
cout << "Enter the message recieved: " << endl;
string msg;
cin >> msg;
msg = xorfun(msg,crc);
for (char i: msg.substr(msg.length() - crclen +1)){
if (i != '0') {
cout << "Error in communication " << endl;
return 0;
}
}
cout << "No errors in message received" << endl;
return 0;
}