在CRC-16 CCITT中将数据从二进制解码为文本,我应该输入一个码字,使用CRC生成器进行编码

decoding data from Binary to text in CRC-16 CCITT, i should input a codeword have it coded using crc generator,

本文关键字:码字 使用 一个 CRC 编码 输入 数据 CCITT CRC-16 二进制 我应该      更新时间:2023-10-16

在发送端输入码字后,用crc生成器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";
}

我假设这里的目标是模拟某种类型的比特流传输,但允许将 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);
}