40

我想使用 boost::crc 以便它的工作方式与PHP 的 crc32()函数完全一样。我尝试阅读可怕的文档,但后来很多头疼我没有取得任何进展。

显然我必须做类似的事情:

int GetCrc32(const string& my_string) {
    return crc_32 = boost::crc<bits, TruncPoly, InitRem, FinalXor,
                   ReflectIn, ReflectRem>(my_string.c_str(), my_string.length());
}

bits应该是 32 .. 其他的东西是什么是个谜。一点帮助?;)

4

5 回答 5

72

Dan Story 和 ergosys 提供了很好的答案(显然我找错地方了,这就是令人头疼的原因)但是当我这样做时,我想为我的问题中的功能提供一个复制和粘贴解决方案,供未来的谷歌用户使用:

uint32_t GetCrc32(const string& my_string) {
    boost::crc_32_type result;
    result.process_bytes(my_string.data(), my_string.length());
    return result.checksum();
}
于 2010-04-04T06:24:44.807 回答
10

您可能想使用crc_32_type而不是使用 crc 模板。该模板是通用的,旨在适应使用广泛变化的参数的各种 CRC 设计,但它们提供了四种内置的预配置 CRC 类型以供常用,包括 CRC16、CCITT、XMODEM 和 CRC32。

于 2010-04-04T06:20:34.487 回答
5

该库包括预定义的 CRC 引擎。我认为您想要的是 crc_32_type。请参阅此示例: http: //www.boost.org/doc/libs/1_37_0/libs/crc/crc_example.cpp

于 2010-04-04T06:20:36.123 回答
4

您是否尝试过使用预定义的crc_32_type

于 2010-04-04T06:21:22.457 回答
4

在此页面上,找到您想要的特定 32 位 CRC,读取所有其他参数: http ://regregex.bbcmicro.net/crc-catalogue.htm

于 2010-04-04T06:22:48.150 回答