首页 > 解决方案 > 在 CRC-16 CCITT 中将数据从二进制解码为文本,我应该输入一个代码字,并使用 crc 生成器对其进行编码,

问题描述

在发送方输入码字后,使用 crc generator poly 对其进行编码,在接收方必须对其进行解码并检查错误,并且可以手动输入错误。这是发送方(没有对输入进行编码)它工作得很好:

#include <string>
#include <bitset>
#include <iostream>
using namespace std;
int main ()
{
    string msg,crc,encoded="";
    cout<<"Enter the message=";
    getline(cin,msg);
    cout<<"Enter the crc generator polynomial=";
    getline(cin,crc);
    int m=msg.length(), n=crc.length();
    encoded+=msg;
    for(int i=1 ; i<=n-1; i++)
     encoded+='0';
     for(int i=0;i <=encoded.length()-n; )
{
   for(int j=0;j<n; j++)
   encoded[i+j]= encoded[i+j]==crc[j]? '0' :'1';
   for(; i<encoded.length() && encoded[i]!='1'; i++);
    }   
    cout<<msg+encoded.substr(encoded.length()-n+1);
    return 0;
}

带有编码输入的发件人(从文本到二进制):

#include <string>
#include <bitset>
#include <iostream>
using namespace std;
int main(){
    string msg,crc,encoded="";
    cout<<"Enter the message=";
    getline(cin,msg);
    cout<<"Enter the crc generator polynomial=";
    getline(cin,crc);
    int m=msg.length(), n=crc.length();
    encoded+=msg;
    for (std::size_t i = 0; i < msg.length(); ++i)
    {

    for(int i=1 ; i<=n-1; i++)
     encoded+='0';
     for(int i=0;i <=encoded.length()-n; )
{
   for(int j=0;j<n; j++)
   encoded[i+j]= encoded[i+j]==crc[j]? '0' :'1';
   for(; i<encoded.length() && encoded[i]!='1'; i++);
    }   
          cout << bitset<8>(msg.c_str()[i]) << endl;

    cout<<msg+encoded.substr(encoded.length()-n+1);
    return 0;
}
}

接收方,我真的尝试了很多,我不知道如何解码它仍然检查错误

#include <iostream>
using namespace std;
int main ()
{
    string crc,encoded;
    cout<<"Enter the message=";
    getline(cin,encoded);
    cout<<"Enter the crc generator polynomial=";
    getline(cin,crc);

    for(int i=0;i <=encoded.length()-crc.length();){

      for(int j=0;j<crc.length();j++)
        encoded[i+j]= encoded[i+j]==crc[j]? '0':'1';
        for(; i<encoded.length() && encoded[i]!='1'; i++);
    }
    for(char i: encoded.substr(encoded.length()-crc.length()+1))
if(i!='0'){
    cout<<"Error in comunication...";
    return 0;

}
cout<<"No error";
}

标签: c++crccrc16

解决方案


我假设这里的目标是模拟某种类型的位流传输,但允许将传统类型(如 8 位和 16 位整数)用于消息和 CRC,它们被转换为用于“传输”和“接收”的位流.

发送方进程可以生成 CRC 并将其附加到消息中,将 msg 转换为二进制字符串并将二进制字符串发送到 std::cout。接收进程可以从标准输入读取(getline),从二进制字符串转换回消息,并检查 CRC 是否有错误。对于 Windows 控制台程序,如下命令:

sndr | rcvr

要引入错误,可以使用会更改一个或多个位的第三个过程:

sndr| chgbit | rcvr

我不知道这将如何与类似 Posix 的系统一起使用。

链接到将字符串转换为二进制字符串的问题和答案:

C++字符串到二进制代码/二进制代码到字符串

将二进制字符串转换为 ASCII 字符串 (C++)

CRC 部分的示例 C++ 代码:

#include <iostream>
#include <string>

typedef unsigned char   uint8_t;
typedef unsigned short uint16_t;

#define POLY (0x1021)

uint16_t crc16(uint8_t * msg, size_t size)
{
uint16_t crc = 0xffffu;                     // initial crc value
size_t i;
    while(size--){
        crc ^= ((uint16_t)*msg++)<<8;       // xor byte into upper 8 bits
        for(i = 0; i < 8; i++)              // cycle 8 bits
            crc = (crc>>15) ? (crc<<1)^POLY : (crc<<1);
    }
    return(crc);
}

int main(int argc, char**argv)
{
    uint16_t crc;
    std::string msg;
    std::cout << "enter message:\n" << std::endl;
    std::getline(std::cin,msg);
    // encode the message
    crc = crc16((uint8_t *)&msg[0], msg.size());
    msg.push_back((char)(crc>>8));
    msg.push_back((char)(crc&0xff));
    // decode a message with no error
    crc = crc16((uint8_t *)&msg[0], msg.size());
    if(crc != 0)
        std::cout << "bug in code" << std::endl;
    // create an error in the message
    msg[msg.size()/2] ^= 0x01;
    // decode a message with error
    crc = crc16((uint8_t *)&msg[0], msg.size());
    if(crc == 0)
        std::cout << "bug in code" << std::endl;
    return(0);
}

推荐阅读