在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生成器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);
}
相关文章:
- 通过套接字[TCP]传输数据 如何在C / C ++中打包多个整数并使用send() recv()传输数据
- luaL_dofile在已知良好的字节码上失败,可以使用未编译的版本
- 当字段可以为null时,如何使用C++接口在Avro中写入数据
- 套接字连接"Operation not permitted"错误,甚至使用升压/平发器根.cpp
- 我可以使用任何好的逻辑来阻止计算机将 O 放在井字游戏中的现有 X 上
- 如何使用位字段将数据从二进制文件复制到结构中?
- 使用同一类中的方法重新分配字段时出现问题
- 在CRC-16 CCITT中将数据从二进制解码为文本,我应该输入一个码字,使用CRC生成器进行编码
- 想要查找二进制码字中出现的 1 或 0 的位置
- 提升 ASIO 套接字使用文件描述符
- 如何将模板缓冲区与 alpha 掩码一起使用
- 对来自多个线程的阻塞套接字使用send()
- 我可以使用非阻塞套接字使用 "sendmsg/recvmsg"将 FD 从一个进程发送到另一个进程吗?
- 如何检测 LZW 解码的码字长度
- 在Windows上创建一个SSL套接字,该套接字使用WinCrypt证书进行身份验证
- 将char*转换为boost::数组以供套接字使用
- 我可以对多个侦听套接字使用相同的boost::asio::io_service吗
- 提振.Asio:对每个连接/套接字使用“io_service”是件好事吗?
- BSD套接字-使用发送和接收
- 定向码字的矢量量化