C++中的校验和

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19821844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:10:42  来源:igfitidea点击:

Checksum in C++

c++checksum

提问by user899714

I need help with writing a code in C++ to do a 16 bit checksum.

我需要帮助用 C++ 编写代码来执行 16 位校验和。

The idea is to get data input from user (a string), convert it to binary form, then calculate checksum and put it in my data packet. I don't have any checksum type specification... I thought XORing the 16 bits would be a good idea. I have the following class packet given:

这个想法是从用户那里获取数据输入(一个字符串),将其转换为二进制形式,然后计算校验和并将其放入我的数据包中。我没有任何校验和类型规范......我认为对 16 位进行异或将是一个好主意。我有以下课程包:

class packet
{
private:
    string message;
    unsigned int checksum;  // this is a 16 bit checksum
    unsigned int seqNum;

public:
    packet(string str, unsigned int seq);
    void setMessage(string str);
    void setSeqNum(unsigned int num);
    string getMessage();
    int getLength();
    unsigned int calculateChecksum();
    int getChecksum();
    bool getSeqNum();
};

I have to calculate my checksum here given the above:

鉴于上述情况,我必须在这里计算我的校验和:

packet::packet(string str, unsigned int seq)
{
    this->message = str;
    this->seqNum = seq;
    // calculate checksum here
    checksum = calculateChecksum(); //HOW TO CODE THIS FUNCTION?
}

Please help. I am new to C++ and C.

请帮忙。我是 C++ 和 C 的新手。

回答by nio

You could use check sum used in IPv4 header, it's 16bit:

您可以使用 IPv4 标头中使用的校验和,它是 16 位:

http://en.wikipedia.org/wiki/IPv4_header_checksum

http://en.wikipedia.org/wiki/IPv4_header_checksum

Or check out CRC16: http://en.wikipedia.org/wiki/Crc16

或查看 CRC16:http: //en.wikipedia.org/wiki/Crc16

You could find more algorighms if you googled a bit around:

如果你在谷歌上搜索一下,你可以找到更多的算法:

http://en.wikipedia.org/wiki/List_of_hash_functions

http://en.wikipedia.org/wiki/List_of_hash_functions

http://en.wikipedia.org/wiki/Category:Checksum_algorithms

http://en.wikipedia.org/wiki/Category:Checksum_algorithms

To implement your function, just first sequentially convert every pair of characters in your String messageand feed it to the checksum algorithm.

要实现您的功能,只需首先依次转换您的每对字符String message并将其提供给校验和算法。

If you have an odd number of bytes, you have to zero pad the last byte properly (check docs for your individual algorithm you did choose, or just compute the 16bit value of the last pair as the last missing character was zero).

如果您有奇数个字节,则必须正确填充最后一个字节(检查您选择的单个算法的文档,或者只计算最后一对的 16 位值,因为最后一个缺少的字符为零)。

After you have processed the message, add the seq number to the has and store your checksum result in your checksumvariable.

处理完消息后,将序列号添加到 has 并将校验和结果存储在checksum变量中。

If you want to test if you did a mistake in the process, many algorithms (cyclic) do output zero result after you hashed the whole message including its CRC. If you get zero it means the message is not corrupt or your algorithm works correctly when you test it.

如果您想测试您是否在此过程中犯了错误,许多算法(循环)在对包括其 CRC 在内的整个消息进行哈希处理后确实会输出零结果。如果您得到零,则表示消息没有损坏,或者您的算法在您测试时正常工作。

回答by Arjen

implementing a function goes as follows:

实现一个功能如下:

unsigned int packet::calculateChecksum()
{
    // here you can use the members of packet, make a for-loop and other things.
}

Is the constructor already implemented?

构造函数是否已经实现?