I am using CRC for the first time (boost::crc_32_type
), and I noticed that calling the process_bytes()
method twice with the same parameters I get different results. Is it normal?
#include <boost/crc.hpp>
#include <ios> 开发者_StackOverflow中文版 // for std::ios_base, etc.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string data1, data2;
boost::crc_32_type result1, result2;
data1 = "This is a test string";
data2 = data1;
result1.process_bytes(data1.c_str(), data1.length());
cout << "result1: " << hex << uppercase << result1.checksum() << endl;
result1.process_bytes(data1.c_str(), data1.length());
cout << "result1: " << hex << uppercase << result1.checksum() << endl;
result2.process_bytes(data1.c_str(), data1.length());
cout << "result2: " << hex << uppercase << result2.checksum() << endl;
return 0;
}
This is the output:
result1: 2DB69898
result1: E29C91
result2: 2DB69898
According to the manual, checksum()
returns "the CRC checksum of the data passed in so far". So, the second checksum is the checksum of the concatenation of data1 with itself and thus naturally different from the checksum of data1.
I'll throw a random rock... You aren't resetting the state of result1
, so the checksum you are calculating the second time is of "This is a test stringThis is a test string". Mmmh... Yes... There should be a crc_32_type.reset()
.
精彩评论