无法在 C++ 中的 CRC 实现中获得以下部分
unable to get the following part in CRC implementation in c++
所以我指的是 Geeks For Geeks 在数据通信中实现 CRC
这是代码:-
#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;
}
我得到了除以下代码之外的所有代码:-
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;
}
有人可以帮助我理解这部分让我们说数据 = 1010101010
和密钥 = 11001.
问题:- 这有什么用,就像这部分到底在做什么:-
std::string(pick, '0')
任何帮助将不胜感激。
std::string(pick, '0')
是字符串构造函数,它创建一个长度为 pick
并用 '0'
填充的字符串。
在其他方面,它是以下代码的缩写形式:-
string zeroes_to_be_added;
zeroes_to_be_added.resize(pick);
for (int i = 0; i < pick; i++)
zeroes_to_be_added[pick] = '0';
所以我指的是 Geeks For Geeks 在数据通信中实现 CRC
这是代码:-
#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;
}
我得到了除以下代码之外的所有代码:-
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;
}
有人可以帮助我理解这部分让我们说数据 = 1010101010 和密钥 = 11001.
问题:- 这有什么用,就像这部分到底在做什么:-
std::string(pick, '0')
任何帮助将不胜感激。
std::string(pick, '0')
是字符串构造函数,它创建一个长度为 pick
并用 '0'
填充的字符串。
在其他方面,它是以下代码的缩写形式:-
string zeroes_to_be_added;
zeroes_to_be_added.resize(pick);
for (int i = 0; i < pick; i++)
zeroes_to_be_added[pick] = '0';