循环冗余算法实现(CRC)

#include<iostream>
#include <stdio.h>
#include <string>
#include <stdlib.h>
using namespace std;
string generator(string message, string G);//循环冗余算法
void verifier(string message, string G);//用生成多项式判断接收信息是否正确
string modtwo(string message, string G);//除模算法实现

void alter(int num,string& message,string G)//制造某个比特位传输错误的传输现象

string generator(string message, string G)
{
    for (int i = 1; i < size(G); i++)//补零
    {
        message = message + '0';
    }
    cout << "加上零之后的message是:" << message << endl;
    string temp = modtwo(message, G);
    cout << "计算出来的模是:" << temp << endl;
    for (int i =0; i<temp.size();i++)
    {
        message[message.size()+i-temp.size()] = temp[i];
    }
    cout << "最后传输的message变成了:" << message << endl;
    return message;
}
string modtwo(string message,string G)
{
    string temp;
    string a = message;
    if (a.size() < G.size())
    {
        bool flag= 0;
        for (int j = 0; j < a.size(); j++)
        {
            if (a[j] != '0')
            {
                flag = 1;
            }
        }
        if (flag == 0)
        {
            return "0";
        }
        while (a[0] == 0)
        {
            a.erase(0, 1);
        }
        return a;
    }
    else
    {
        for (int i = 0; i < G.size(); i++)
        {
            if (G[i] != a[i])
            {
                temp = temp + '1';
            }
            else
            {
                temp = temp + '0';
            }
        }
        while (temp[0] == '0')
        {
            temp.erase(0, 1);
        }
        for (int i = G.size(); i < a.size(); i++)
        {
            temp = temp + a[i];
        }
        a = temp;
        return modtwo(a, G);
    }
}
void verifier(string message, string G)
{
    string a = modtwo(message, G);
    bool flag = 0;
    for (int i = 0; i < a.size(); i++)
    {
        if (a[i] != '0')
        {
            flag = 1;
        }
    }
    if (flag == 0)
    {
        cout << "信息传输无误" << endl;
    }
    else
    {
        cout << "信息传输错误" << endl;
    }
}
void alter(int num,string& message,string G)
{
    if (message[num + 1] == '0')
    {
        message[num + 1] = '1';
    }
    else
    {
        message[num + 1] = '0';
    }
    cout << "再判断接受信息的正误:";
    verifier(message, G);
}
int main()
{
    string message, G;
    int x;
    cout << "请输入待传输信息:";
    getline(cin, message);
    cout << "请输入生成多项式:";
    getline(cin, G);
    message=generator(message, G);
    verifier(message, G);
    cout << "传输过程使得信息中出现错误的比特位是:" << endl;
    cin >> x;
    alter(x, message,G);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是C++实现循环冗余校验(CRC算法的示例代码: ```c++ #include <iostream> #include <string> #include <vector> typedef unsigned char byte; typedef unsigned int uint; class CRC32 { public: CRC32() { table.resize(256); init_table(); reset(); } void reset() { crc = 0xFFFFFFFF; } void update(byte* data, uint len) { for (uint i = 0; i < len; i++) { crc = (crc >> 8) ^ table[(crc ^ data[i]) & 0xFF]; } } uint get_crc() { return crc ^ 0xFFFFFFFF; } private: uint crc; std::vector<uint> table; void init_table() { uint poly = 0xEDB88320; for (uint i = 0; i < 256; i++) { uint c = i; for (int j = 0; j < 8; j++) { if (c & 1) { c = poly ^ (c >> 1); } else { c >>= 1; } } table[i] = c; } } }; int main() { std::string data = "hello world"; byte* data_ptr = reinterpret_cast<byte*>(const_cast<char*>(data.c_str())); uint len = data.length(); CRC32 crc; crc.update(data_ptr, len); std::cout << "CRC32: " << std::hex << crc.get_crc() << std::endl; return 0; } ``` 在这个示例代码中,我们使用了一个CRC32的实现,它使用了一个32位的CRC多项式(0xEDB88320),并且在初始化时生成了一个预处理表(table)来加速计算。在使用时,我们先调用reset()函数来重置CRC值,然后使用update()函数来更新CRC值,最后调用get_crc()函数来获取最终的CRC校验码。 注意:在实际使用中,我们可能需要根据具体的应用场景来选择合适的CRC多项式和预处理表。同时,由于多项式和预处理表的不同,不同的CRC实现之间可能会产生不同的校验码。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值