实现 CHECKSUM 的 C/C++ 程序
校验和是一种错误检测方法,可在数据/消息从发送方传输到接收方时检测数据/消息中的错误。此方法由更高层协议使用,并利用发送方的校验和生成器和接收方的校验和检查器。
例子:
Input: sent_message = “10101111”, rec_message = “10101101”, block_size = 8
Output: Error
Explaination: Since the 7th bit in the sent_message and the rec_message is different, the final checksum value is not equal to zero denoting that some error has occurred during transmission.
Input: sent_message = “10000101011000111001010011101101”, rec_message = “10000101011000111001010011101101”, block_size = 8
Output: No Error
方法:给定的问题可以分为以下两个部分:
- 可以使用以下步骤生成发送方消息的校验和值:
- 将消息分成给定块大小的二进制字符串。
- 所有二进制字符串相加得到总和。
- 表示和的二进制字符串的 One's Complement 是所需的校验和值。
- 检查接收到的消息的值(即rec_message + senders_checksum )是否等于0 。
- 接收消息的校验和可以与上述过程中计算的校验和类似地计算。
- 如果校验和值为0 ,则消息正确传输,没有错误,否则,在传输过程中发生了错误。
下面是上述方法的实现:
C++
// C++ implementation of the above approach
#include
using namespace std;
// Function to find the One's complement
// of the given binary string
string Ones_complement(string data)
{
for (int i = 0; i < data.length(); i++) {
if (data[i] == '0')
data[i] = '1';
else
data[i] = '0';
}
return data;
}
// Function to return the checksum value of
// the give string when divided in K size blocks
string checkSum(string data, int block_size)
{
// Check data size is divisible by block_size
// Otherwise add '0' front of the data
int n = data.length();
if (n % block_size != 0) {
int pad_size = block_size - (n % block_size);
for (int i = 0; i < pad_size; i++) {
data = '0' + data;
}
}
// Binary addition of all blocks with carry
string result = "";
// First block of data stored in result variable
for (int i = 0; i < block_size; i++) {
result += data[i];
}
// Loop to calculate the block
// wise addition of data
for (int i = block_size; i < n; i += block_size) {
// Stores the data of the next bloack
string next_block = "";
for (int j = i; j < i + block_size; j++) {
next_block += data[j];
}
// Stores the binary addition of two blocks
string additions = "";
int sum = 0, carry = 0;
// Loop to calculate the binary addition of
// the current two blocls of k size
for (int k = block_size - 1; k >= 0; k--) {
sum += (next_block[k] - '0')
+ (result[k] - '0');
carry = sum / 2;
if (sum == 0) {
additions = '0' + additions;
sum = carry;
}
else if (sum == 1) {
additions = '1' + additions;
sum = carry;
}
else if (sum == 2) {
additions = '0' + additions;
sum = carry;
}
else {
additions = '1' + additions;
sum = carry;
}
}
// After binary add of two blocks with carry,
// if carry is 1 then apply binary addition
string final = "";
if (carry == 1) {
for (int l = additions.length() - 1; l >= 0;
l--) {
if (carry == 0) {
final = additions[l] + final;
}
else if (((additions[l] - '0') + carry) % 2
== 0) {
final = "0" + final;
carry = 1;
}
else {
final = "1" + final;
carry = 0;
}
}
result = final;
}
else {
result = additions;
}
}
// Return One's complements of result value
// which represents the required checksum value
return Ones_complement(result);
}
// Function to check if the received message
// is same as the senders message
bool checker(string sent_message,
string rec_message,
int block_size)
{
// Checksum Value of the senders message
string sender_checksum
= checkSum(sent_message, block_size);
// Checksum value for the receivers message
string receiver_checksum = checkSum(
rec_message + sender_checksum, block_size);
// If receivers checksum value is 0
if (count(receiver_checksum.begin(),
receiver_checksum.end(), '0')
== block_size) {
return true;
}
else {
return false;
}
}
// Driver Code
int main()
{
string sent_message
= "10000101011000111001010011101101";
string recv_message
= "10000101011000111001010011101101";
int block_size = 8;
if (checker(sent_message,
recv_message,
block_size)) {
cout << "No Error";
}
else {
cout << "Error";
}
return 0;
}
输出
No Error
时间复杂度: O(N)
辅助空间: O(block_size)