CRC检错码的生成和校验(c++实现)

功能

  1. 输入任意编码及生成多项式,输出其十六进制CRC编码;
  2. 输入给定编码及生成多项式,判断其是否出错
     

代码

#include<iostream>
#include<string>
#include<math.h>
#include<vector>
#include<algorithm>
#define MAX 100
using namespace std;

//判断是否是2的幂次方
int is2n(string bin)
{
    int count=0;
    for(int i=0; i<bin.size(); i++)
    {
        if(bin[i]=='1')
            count++;
    }
    return count==1?1:0;
}
//求数对应的十六进制符号
char hexNumber(int num)
{
    char hex;
    if(num>=0&&num<=9)
        return num+'0';
    else
    {
        switch(num)
        {
        case 10:
            return 'A';
        case 11:
            return 'B';
        case 12:
            return 'C';
        case 13:
            return 'D';
        case 14:
            return 'E';
        case 15:
            return 'F';
        default:
            cout<<"error"<<endl;
            return '#';
        }
    }
}
//将十进制串转成二进制
string dec2Bin(int dec)
{
    string bin="";
    while(dec)
    {
        int remainer=dec%2;
        bin.insert(0,to_string(remainer));
        dec=dec/2;
    }
    return bin;
}

//二进制串转十六进制
string bin2hex(string bin)
{
    string hex="";//十六进制串

    //前面补0
    int cnt=0;//补0的个数
    if(bin.size()%4!=0)
    {
        cnt=4-(bin.size()%4);
    }

    for(int i=0; i<cnt; i++)
        bin.insert(0,"0");

    //二进制转十六进制
    for(int i=0; i<bin.size(); i+=4)
    {
        int sum=0;

        for(int j=0; j<4; j++)
        {
            sum+=(bin[i+j]-'0')*pow(2,3-j);//注意求的时候从右往左,因此用3-j
            //cout<<i+j<<"\t"<<(bin[i+j]-'0')*pow(2,3-j)<<endl;
        }
        hex.push_back(hexNumber(sum));
    }
    return hex;
}

//两个二进制串做异或
string XOR(string a,string b)
{
    //逆序让两串最低位对齐
    reverse(a.begin(),a.end());
    reverse(b.begin(),b.end());

    int i;
    string res;
    for(i=0;i<a.size()&&i<b.size();i++)
    {
        if(a[i]==b[i])
            res.insert(res.begin(),'0');
        else
            res.insert(res.begin(),'1');
    }
    //剩余的串补上
    if(i<a.size())
    {
        for(;i<a.size();i++)
            res.insert(res.begin(),a[i]);
    }
    if(i<b.size())
    {
        for(;i<b.size();i++)
            res.insert(res.begin(),b[i]);
    }
    return res;
}
//CRC模2除,返回余数
string division(string data,string Gx)
{
    string remainder;//余数
    string temp=data.substr(0,Gx.size());
    remainder=XOR(temp,Gx);

    for(int i=Gx.size();i<data.size();i++)
    {
        remainder.erase(0,1);
        remainder.push_back(data[i]);

        //余数第一位是1,则商1,计算下一次余数
        //余数第一位是0,则下一次余数不变
        if(*remainder.begin()=='1')
        {
            remainder=XOR(remainder,Gx);
        }
    }
    //cout<<"remainder : "<<remainder<<endl;

    return remainder;
}

//CRC编码
string CRC(string data,string Gx)
{
    string code;//CRC校验码

    //数据左移
    data.insert(data.end(),Gx.size()-1,'0');

    /*模2除法*/
    string remainder=division(data,Gx);

    /*data和余数模2加*/
    code=XOR(data,remainder);
    //cout<<"code : "<<code<<endl;

    return code;
}

//检错
int Detect(string code,string Gx)
{
    string remainder=division(code,Gx);

    //余数为0,结果正确
    if(remainder.find('1')==string::npos)
    {
        cout<<endl<<"**** No error is detected. ****"<<endl;
        return 1;
    }
    else
    {
        cout<<endl<<"****** Error! ******"<<endl;
        return 0;
    }
}

int main()
{
    string data;
    string Gx;
    cout<<"Please choose ( 1:Encoding 2:Error-Detecting 3:Quit )"<<endl;
    int choice;
    while(cin>>choice)
    {
        string res;
        switch(choice)
        {
        case 1:
            cout<<"Please input the codeword you are sending"<<endl;
            cin>>data;
            cout<<"Please input the coefficients of generator polynomial"<<endl;
            cin>>Gx;
            res=CRC(data,Gx);
            cout<<"CRC code in hexadecimal : "<<bin2hex(res)<<endl;
            break;
        case 2:
            cout<<"Please input the CRC code you received"<<endl;
            cin>>data;
            cout<<"Please input the coefficients of generator polynomial"<<endl;
            cin>>Gx;
            Detect(data,Gx);
            break;
        case 3:
            return 0;
        default:
            cout<<"Wrong number"<<endl;
        }
        cout<<"-----------------------------------------------------------------------"<<endl;
        cout<<"Please choose ( 1:Encoding 2:Error-Detecting 3:Quit )"<<endl;
    }
    return 0;
}
/*
测试数据:
1
1101011111
10011
2
1101001
1011
2
1101000
1011
*/

 

运行结果

在这里插入图片描述

  • 1
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值