在发送方输入代码字后,使用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";
}
发送方进程可以生成CRC并将其附加到消息,将msg转换为二进制字符串,然后将二进制字符串发送到std :: cout。接收方进程可以从标准输入中读取(获取行),从二进制字符串转换回消息,并检查CRC是否存在错误。对于Windows控制台程序,类似以下命令:
sndr | rcvr
为了引入错误,可以使用将改变一个或多个位的第三过程:
sndr| chgbit | rcvr
我不知道如何在类似Posix的系统上使用它。
链接到将字符串转换为二进制字符串或从二进制字符串转换为字符串的问题和答案:
C++ string to binary code / binary code to string
Convert a string of binary into an ASCII string (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);
}