Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- // Function to perform CRC error detection
- string performCRC(const string& data, const string& divisor) {
- string dividend = data;
- int divisorLength = divisor.length();
- int dataLength = data.length();
- // Append zeros to the dividend to match the divisor length
- for (int i = 0; i < divisorLength - 1; i++) {
- dividend += '0';
- }
- // Perform division
- for (int i = 0; i < dataLength; i++) {
- if (dividend[i] == '1') {
- for (int j = 0; j < divisorLength; j++) {
- dividend[i + j] = (dividend[i + j] == divisor[j]) ? '0' : '1';
- }
- }
- }
- // Extract the remainder (CRC)
- string remainder = dividend.substr(dataLength, divisorLength - 1);
- return remainder;
- }
- int main() {
- string data1;
- string divisor;
- string data2;
- cout << "Enter data (7/8 bits): ";
- cin >> data1;
- cout << "Enter divisor: ";
- cin >> divisor;
- string crc1 = performCRC(data1, divisor);
- cout << "CRC (Remainder): " << crc1 << endl;
- cout <<"Enter the recieved packet:";
- cin>>data2;
- string crc2=performCRC(data2,divisor);
- cout<<"New remainder:"<<crc2<<endl;
- if(crc1==crc2){
- cout<<"no error"<<endl;
- }
- else{
- cout<<"error"<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement