1

After inputing codeword in sender's side have it coded with crc generator poly , on the reveiver side it has to be decoded and checked for error , and the error can be input manually. This is the sender side (without coding the input) It worked fine:

#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;
}

sender with coding input(from text to binary):

#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;
}
}

Receiver side, i really tried a lot and i didn't know how to decode it and still check for error

#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";
}
user207421
  • 305,947
  • 44
  • 307
  • 483
  • Could you include some examples of input and output strings in the question? – rcgldr Jun 13 '20 at 16:56
  • @rcgldr for example I input the text hi , and it was coded into Enter the message "hi" Enter the crc generator polynomial=1101 01101000 01101001 and basically the reciever side would only decode h and also if i try with another letter like a or v i would get error even though the message i input in receiver is correct, i modified the code a bit ,should i repost, i searched all over the internet and no use – Oumayma Mejri Jun 14 '20 at 10:44
  • You could do a web search for "CRC16 CCITT" to find code examples. The title states CRC-16, CCITT, which uses the 16 bit remainder of division of a message of bits (with 16 appended zero bits) by a 17 bit polynomial, but your "generator polynomial" has 20 bits. Is doing this with an array of characters each holding 1 bit part of the requirement? It would be simpler to use the normal data types since there are bit level operators than can work on 8, 16, 32, ... bits at a time in parallel. – rcgldr Jun 14 '20 at 16:15
  • @rcgldr I did try , i really am not understanding this subject , actually no , the requirement was only stated to write 3 programms one sender one receiver and fault injector the sender codes the word and the receiver decodes and detects the error , is the 17 bit polynomial fixed ? what is its value , if i enter binary code word it has to be 16 bit and polynomial 17 bit ? can you tell me how to add the polynomial or how to fix that ? – Oumayma Mejri Jun 15 '20 at 08:38
  • @rcgldr at least what is the value of polynomial i have to input, i am really struggling i did a lot of other modifications – Oumayma Mejri Jun 15 '20 at 08:39
  • The polynomial is ix11021. Typically software uses the lower 16 bits, 0x1021. Here is a link to a website about [crc16 ccitt](http://srecord.sourceforge.net/crc16-ccitt.html). I'm finding variations in the crc16 examples that all claim to be crc16-ccitt, (some reverse the bits, some post complement the crc). – rcgldr Jun 15 '20 at 09:31
  • @rcgldr i might sound stupid but if i type that poly it doesn't work i thought i was supposed to add a binary poly when i execute the code when i have to type the crcgenerator poly it has to be binary for it to work, i'm sorry to bother you but i actually emailed you – Oumayma Mejri Jun 15 '20 at 09:51

1 Answers1

0

I'm assuming the goal here is to emulate some type of bit stream transmission, but allow conventional types like 8 and 16 bit integers to be used for the message and CRC, which get converted to bit stream for "transmitting" and "receiving".

A sender process could generate and append a CRC to a message, convert the msg to a binary string and send the binary string to std::cout. A receiver process could read (getline) from standard in, convert from a binary string back to a message, and check the CRC for errors. For Windows console program, a command like:

sndr | rcvr

To introduce error a third process that would change one or more bits could be used:

sndr| chgbit | rcvr

I don't know how this would work with a Posix like system.

Link to questions and answers for converting a string to/from a binary string:

C++ string to binary code / binary code to string

Convert a string of binary into an ASCII string (C++)

Example c++ code for the CRC part:

#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);
}
rcgldr
  • 27,407
  • 3
  • 36
  • 61