[Software Designer Exam Preparation Topic] Verification method and verification code: ensuring data integrity


Soft Test_Software Design Column: Soft Test Software Designer Tutorial


1. The importance of verification methods and verification codes

Checksum methods and checksums play a vital role in data communication and storage to ensure data integrity and accuracy. In the computer technology and software professional technical qualification examination, one of the knowledge points in the software designer examination is the verification method and verification code. This chapter will introduce the importance of parity check codes and check codes in detail, and deepen the understanding by discussing the working principles of parity check codes, Hamming check codes and cyclic redundancy check codes in depth.

1.1 Introduction

Checksums and checksums are techniques used to detect and correct errors that may occur during data transmission or storage. In data communication and storage, data errors may occur due to noise, interference or other reasons. Checksums and checksums detect and correct these errors by adding redundant information to ensure data integrity and accuracy.

1.2 Importance

The integrity and accuracy of data is critical to the proper functioning of computer systems. Errors in data during data transfer may result in system crashes, loss of information, or erroneous results. In the data storage process, if the data is damaged or lost, it may cause the system to fail to operate normally or the data cannot be recovered.

Therefore, the importance of the verification method and verification code cannot be ignored. They help us detect and correct errors in data transmission or storage, improving system reliability and stability.

In the following chapters, we will focus on how parity-check codes, Hamming-check codes, and cyclic redundancy check codes work, and explore their application scenarios.

Note: The content of this chapter will describe the principle from the underlying source code, and introduce the knowledge points in the form of code examples and comments, which is easy to understand and practical application.


2. Parity code

2.1 Working principle

Parity code is a simple and commonly used checking method to detect errors during data transmission. Its working principle is based on the concept of parity. By adding a parity bit to the data, the total number of data bits is odd or even, so as to detect single bit errors that may occur during transmission.

The parity code works as follows:

  1. At the sending end, the data to be transmitted is encoded according to fixed rules, usually the highest bit of each byte is used as the parity bit.
  2. At the receiving end, after receiving the data, check the highest bit of each byte with the remaining bits to calculate the parity of the check bit.
  3. If the received data has a single bit error during transmission, the parity of the check bit will be inconsistent with that of the sender, so that the error can be detected.

2.2 Calibration method

The parity check code check method is as follows:

  1. Odd parity: Set the total number of data bits to an odd number to ensure that when the parity bit is 1, the number of 1s in the data bit is an odd number.
  2. Even parity: Set the total number of data bits to an even number to ensure that when the parity bit is 1, the number of 1s in the data bit is an even number.

The choice of parity check code depends on specific application scenarios and requirements. During the data transmission process, the sending end and the receiving end need to agree on the verification method to be used in advance.

2.3 Application scenarios

Parity codes are widely used in data transmission, especially in serial communication and storage media. Here are some common application scenarios:

  • Data Verification in RS-232 Serial Communication
  • Data checksums in disk storage
  • Data verification in network communication

Parity codes, while simple, have utility in some simple data transmission scenarios. However, it cannot detect multiple bit errors or the location of errors, so in scenarios where data integrity is critical, other stronger checksums may need to be used. In the next chapters, we will introduce Hamming check codes and cyclic redundancy check codes, which can provide a higher level of data integrity protection.

Calibration method advantage shortcoming
Odd parity Simple implementation, suitable for small-scale data transfer Unable to detect multiple bit errors
even parity Simple implementation, suitable for small-scale data transfer Unable to detect multiple bit errors

The above table summarizes the advantages and disadvantages of parity-check codes to help readers better understand their scope of application and limitations.

In the next section, we will introduce the Hamming check code, which is a more powerful check method capable of detecting and correcting multiple bit errors.


3. Hamming check code

Hamming check code is a common check code used to detect and correct errors in data transmission. It implements data integrity checks by adding redundant bits. This chapter will introduce the working principle, verification method and application scenarios of the Hamming check code in detail.

3.1 Working principle

The Hamming check code is based on the concept of Hamming distance, which refers to the number of different characters in corresponding positions between two equal-length strings. According to the characteristics of Hamming distance, Hamming code can detect multi-bit errors, and can correct errors according to the position of redundant bits.

The working principle of the Hamming check code is as follows:

  1. The original data is divided into two parts: data bits and redundant bits.
  2. The number and location of redundant bits are determined by the checksum algorithm.
  3. The value of the redundant bit is obtained by calculating the parity of the corresponding position in the data bit.
  4. The receiver calculates the parity bit based on the received data bits and redundant bits, and compares it with the received redundant bits to detect and correct errors.

3.2 Calibration method

The verification method of the Hamming check code is as follows:

  1. Determine the number of data bits and the number of redundant bits.
  2. Calculate the value of the redundant bits and add it to the data bits.
  3. The sender sends data with a checksum.
  4. The receiver calculates the parity bit based on the received data and compares it with the received redundant bit.

The verification method of the Hamming check code can be illustrated by the following code example:

#include <iostream>
#include <bitset>

// 计算海明校验码
std::bitset<8> calculateHammingCode(std::bitset<4> data) {
    std::bitset<8> hammingCode;
    
    // 计算冗余位的值
    hammingCode[2] = data[0] ^ data[1] ^ data[3];
    hammingCode[4] = data[0] ^ data[2] ^ data[3];
    hammingCode[5] = data[1] ^ data[2] ^ data[3];
    hammingCode[6] = data[0];
    hammingCode[7] = data[1];
    hammingCode[8] = data[3];
    
    return hammingCode;
}

int main() {
    std::bitset<4> data("1010");
    std::bitset<8> hammingCode = calculateHammingCode(data);
    
    std::cout << "Data: " << data << std::endl;
    std::cout << "Hamming Code: " << hammingCode << std::endl;
    
    return 0;
}

In the above code, we use a 4-bit data bit to calculate the Hamming checksum. By calculating the value of the redundant bits and adding them to the data bits, an 8-bit Hamming check code is finally obtained.

3.3 Application scenarios

Hamming check codes are widely used in the field of data transmission and storage, especially in embedded systems. The following are some application scenarios of Hamming check code:

  1. Data transmission: Hamming check code can be used to detect and correct errors in data transmission to ensure data integrity.
  2. Storage systems: Hamming check codes can be used to detect and correct data corruption in storage systems, improving data reliability.
  3. Communication protocol: Hamming check code can be used in network communication protocols to ensure the accuracy of data during transmission.

To sum up, the Hamming check code is an important check code, and the data integrity check is realized by adding redundant bits. It has the ability to detect and correct multi-bit errors, and has a wide range of applications in data transmission and storage.

The next chapter will introduce the working principle and application of the cyclic redundancy check code.


4. Cyclic Redundancy Check Code (CRC)

Cyclic Redundancy Check (CRC) is a commonly used data verification method for detecting and correcting errors in data transmission or storage. In the software designer exam, there are important investigations on the working principle and application scenarios of CRC.

4.1 Working principle

CRC通过生成多项式除法来计算校验码,将数据按位进行异或运算,得到校验码后附加在数据后面。接收端在接收到数据后,再次进行异或运算,如果结果为0,则说明数据传输或存储过程中没有出现错误。

CRC的工作原理可以通过以下步骤来理解:

  1. 选择一个生成多项式,通常为一个二进制数。
  2. 将数据按位进行异或运算。
  3. 将异或运算结果作为除数,与生成多项式进行除法运算。
  4. 将余数作为校验码附加在数据后面。

4.2 校验方法

CRC的校验方法可以分为以下几个步骤:

  1. 选择一个合适的生成多项式,根据实际需求和数据长度进行选择。
  2. 将数据按位进行异或运算,得到异或运算结果。
  3. 将异或运算结果与生成多项式进行除法运算,得到余数。
  4. 将余数作为校验码附加在数据后面。

4.3 应用场景

CRC广泛应用于数据通信、存储和校验等领域,具体的应用场景包括:

  1. 数据传输:在网络通信、串口通信等场景中,通过附加CRC校验码来确保数据的完整性和准确性。
  2. 存储校验:在存储设备中,通过对数据进行CRC校验,可以检测存储介质中的数据错误,提高数据的可靠性。
  3. 文件校验:在文件传输过程中,通过计算文件的CRC校验码,可以确保文件的完整性,防止数据丢失或被篡改。

4.4 综合示例

下面是一个使用C语言实现CRC校验的示例代码:

#include <stdio.h>

unsigned int crc32(unsigned char *data, int length) {
    
    
    unsigned int crc = 0xFFFFFFFF;
    unsigned int table[256] = {
    
    0};

    // 生成CRC表
    for (int i = 0; i < 256; i++) {
    
    
        unsigned int temp = i;
        for (int j = 0; j < 8; j++) {
    
    
            if (temp & 1) {
    
    
                temp = (temp >> 1) ^ 0xEDB88320;
            } else {
    
    
                temp >>= 1;
            }
        }
        table[i] = temp;
    }

    // 计算CRC校验码
    for (int i = 0; i < length; i++) {
    
    
        crc = (crc >> 8) ^ table[(crc ^ data[i]) & 0xFF];
    }

    return crc ^ 0xFFFFFFFF;
}

int main() {
    
    
    unsigned char data[] = {
    
    0x01, 0x02, 0x03, 0x04};
    int length = sizeof(data) / sizeof(data[0]);

    unsigned int crc = crc32(data, length);
    printf("CRC: 0x%08X\n", crc);

    return 0;
}

该示例代码实现了一个CRC32校验的函数crc32,通过传入待校验的数据和数据长度,计算得到CRC校验码,并输出结果。

4.5 对比表格

下表列出了奇偶校验码、海明校验码和循环冗余校验码的主要特点和适用场景:

校验方法 工作原理 优点 缺点 应用场景
奇偶校验码 检测数据中1的个数 实现简单,计算效率高 无法检测偶数个错误位 串口通信、键盘输入等
海明校验码 添加冗余位进行校验 可纠正单个错误位,可检测多个错误位 冗余位增加数据长度,计算复杂度较高 存储介质、数据传输等
循环冗余校验 生成多项式除法进行校验 可检测多个错误位,计算复杂度适中 需要选择合适的生成多项式 网络通信、存储校验、文件传输等

通过对比表格,可以看出不同的校验方法在实际应用中具有不同的特点和适用场景,根据实际需求选择合适的校验方法可以有效保障数据的完整性和准确性。

以上是关于循环冗余校验码(CRC)的详细介绍,包括其工作原理、校验方法、应用场景以及一个使用C语言实现的示例代码。在软件设计师考试中,对CRC的理解和应用将为你在软件设计领域的发展提供有力的支持。


5. 校验方法和校验码的综合应用示例

在软件设计师考试中,校验方法和校验码是确保数据完整性的重要概念。本章将通过一个综合的示例来展示校验方法和校验码在实际应用中的使用。

5.1 示例背景

假设我们正在开发一个嵌入式系统,其中包含一个传感器模块,用于采集环境温度数据。传感器模块通过串口将数据传输给主控制器。为了确保传输的数据完整性,我们需要在传输过程中使用校验方法和校验码进行验证。

5.2 奇偶校验码的应用

奇偶校验码是一种简单的校验方法,通过检查数据中的奇偶位来验证数据的正确性。在我们的示例中,我们可以使用奇偶校验码来验证传感器数据的正确性。

#include <stdio.h>

// 奇偶校验方法
int parityCheck(unsigned char data) {
    
    
    int count = 0;
    while (data) {
    
    
        count += data & 1;
        data >>= 1;
    }
    return count % 2;
}

int main() {
    
    
    unsigned char sensorData = 0b11001010; // 假设传感器采集到的数据
    int parity = parityCheck(sensorData);

    printf("传感器数据: 0x%02X\n", sensorData);
    printf("奇偶校验位: %d\n", parity);

    return 0;
}

上述示例代码中,我们定义了一个parityCheck函数来进行奇偶校验。通过对传感器数据进行奇偶校验,我们可以得到一个校验位,用于验证数据的完整性。

5.3 海明校验码的应用

海明校验码是一种更强大的校验方法,它可以检测并纠正数据中的错误。在我们的示例中,我们可以使用海明校验码来提高数据传输的可靠性。

#include <stdio.h>

// 海明校验方法
int hammingCheck(unsigned char data) {
    
    
    int p1 = (data >> 6) & 1;
    int p2 = (data >> 5) & 1;
    int p3 = (data >> 4) & 1;
    int p4 = (data >> 2) & 1;

    int d1 = (data >> 3) & 1;
    int d2 = (data >> 1) & 1;
    int d3 = data & 1;

    int h1 = (p1 + p2 + d1) % 2;
    int h2 = (p1 + p3 + d2) % 2;
    int h3 = (p2 + p3 + d3) % 2;

    int error = h1 * 4 + h2 * 2 + h3;

    return error;
}

int main() {
    
    
    unsigned char sensorData = 0b11001010; // 假设传感器采集到的数据
    int error = hammingCheck(sensorData);

    printf("传感器数据: 0x%02X\n", sensorData);
    printf("校验错误位: %d\n", error);

    return 0;
}

上述示例代码中,我们定义了一个hammingCheck函数来进行海明校验。通过对传感器数据进行海明校验,我们可以检测并纠正数据中的错误。

5.4 循环冗余校验码的应用

循环冗余校验码(CRC)是一种广泛应用于数据通信和存储领域的校验方法,它通过计算数据的多项式余数来验证数据的完整性。在我们的示例中,我们可以使用CRC来进行数据传输的校验。

#include <stdio.h>

#define CRC8_POLYNOMIAL 0x07

// CRC校验方法
unsigned char crcCheck(unsigned char *data, int length) {
    
    
    unsigned char crc = 0;
    for (int i = 0; i < length; i++) {
    
    
        crc ^= data[i];
        for (int j = 0; j < 8; j++) {
    
    
            if (crc & 0x80) {
    
    
                crc = (crc << 1) ^ CRC8_POLYNOMIAL;
            } else {
    
    
                crc <<= 1;
            }
        }
    }
    return crc;
}

int main() {
    
    
    unsigned char sensorData[] = {
    
    0b11001010, 0b10101010}; // 假设传感器采集到的数据
    int length = sizeof(sensorData) / sizeof(sensorData[0]);
    unsigned char crc = crcCheck(sensorData, length);

    printf("传感器数据: ");
    for (int i = 0; i < length; i++) {
    
    
        printf("0x%02X ", sensorData[i]);
    }
    printf("\n");
    printf("CRC校验码: 0x%02X\n", crc);

    return 0;
}

上述示例代码中,我们定义了一个crcCheck函数来进行CRC校验。通过计算传感器数据的CRC校验码,我们可以验证数据的完整性。

5.5 校验方法对比

下表总结了奇偶校验码、海明校验码和循环冗余校验码的特点和适用场景。

校验方法 特点 适用场景
奇偶校验码 简单易实现,只能检测错误,无法纠正 数据传输中对数据完整性要求不高的场景
海明校验码 可以检测并纠正错误,但计算复杂度较高 数据传输中对数据完整性要求较高的场景
循环冗余校验码 可以检测并纠正错误,计算复杂度相对较低,应用广泛 数据通信、存储等对数据完整性要求较高的场景

通过以上对比,我们可以根据实际需求选择合适的校验方法来确保数据的完整性。

以上示例展示了校验方法和校验码在嵌入式系统中的应用。了解这些校验方法的工作原理和应用场景,有助于我们在软件设计中选择合适的校验方法,提高数据传输的可靠性和安全性。

注意:以上示例代码仅为演示用途,实际应用中可能需要根据具体情况进行修改和优化。


结语

感谢你花时间阅读这篇博客,我希望你能从中获得有价值的信息和知识。记住,学习是一个持续的过程,每一篇文章都是你知识体系的一部分,无论主题是什么,都是为了帮助你更好地理解和掌握软件设计的各个方面。

如果你觉得这篇文章对你有所帮助,那么请不要忘记收藏和点赞,这将是对我们最大的支持。同时,我们也非常欢迎你在评论区分享你的学习经验和心得,你的经验可能会对其他正在学习的读者有所帮助。

无论你是正在准备软件设计师资格考试,还是在寻求提升自己的技能,我们都在这里支持你。我期待你在软件设计师的道路上取得成功,无论你的目标是什么,我都在这里支持你。

再次感谢你的阅读,期待你的点赞和评论,祝你学习顺利,未来充满可能!


阅读我的CSDN主页,解锁更多精彩内容:泡沫的CSDN主页
在这里插入图片描述

Guess you like

Origin blog.csdn.net/qq_21438461/article/details/132006749