So i was referring Geeks For Geeks for the implementation of CRC in Data Communication
Here is the code:-
#include <bits/stdc .h>
using namespace std;
string xor1(string a, string b)
{
string result = "";
int n = b.length();
for (int i = 1; i < n; i )
{
if (a[i] == b[i])
result = "0";
else
result = "1";
}
return result;
}
string mod2div(string divident, string divisor)
{
int pick = divisor.length();
string tmp = divident.substr(0, pick);
int n = divident.length();
while (pick < n)
{
if (tmp[0] == '1')
tmp = xor1(divisor, tmp) divident[pick];
else
tmp = xor1(std::string(pick, '0'), tmp) divident[pick];
pick = 1;
}
if (tmp[0] == '1')
tmp = xor1(divisor, tmp);
else
tmp = xor1(std::string(pick, '0'), tmp);
return tmp;
}
void encodeData(string data, string key)
{
int l_key = key.length();
string zeroes_to_be_added;
for(int i=0;i<l_key-1;i )
zeroes_to_be_added = zeroes_to_be_added "0";
string appended_data = (data zeroes_to_be_added);
string remainder = mod2div(appended_data, key);
string codeword = data remainder;
cout << "Remainder : " << remainder << "\n";
cout << "Encoded Data (Data Remainder) :" << codeword << "\n";
}
int main()
{
string data,key;
cout<<"enter the data to be send to the receiver side from the sender side"<<endl;
cin>>data;
cout<<"Enter the key value that is present on both sender and reciever side"<<endl;
cin>>key;
encodeData(data, key);
return 0;
}
i get all the code except the below one :-
string mod2div(string divident, string divisor)
{
int pick = divisor.length();
string tmp = divident.substr(0, pick);
int n = divident.length();
while (pick < n)
{
if (tmp[0] == '1')
tmp = xor1(divisor, tmp) divident[pick];
else
tmp = xor1(std::string(pick, '0'), tmp) divident[pick];
pick = 1;
}
if (tmp[0] == '1')
tmp = xor1(divisor, tmp);
else
tmp = xor1(std::string(pick, '0'), tmp);
return tmp;
}
could someone can help me in getting to understand this part lets say for the data = 1010101010 and the key = 11001.
issue:- What is the use of this like what exactly this part is doing:-
std::string(pick, '0')
Any help will be appreciated.
CodePudding user response:
std::string(pick, '0')
is the string constructor creating a string of length pick
filled with '0'
.
In other ways it's the short form of the following code :-
string zeroes_to_be_added;
zeroes_to_be_added.resize(pick);
for (int i = 0; i < pick; i )
zeroes_to_be_added[pick] = '0';