C++ 如何使用 boost::crc?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2573726/
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:57:56  来源:igfitidea点击:

How to use boost::crc?

c++boostcrc

提问by Thomas Bonini

I want to use boost::crc so that it works exactly like PHP's crc32()function. I tried reading the horrible documentationand many headaches later I haven't made any progress.

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

Apparently I have to do something like:

显然我必须做这样的事情:

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

bitsshould be 32.. What the other things are is a mystery. A little help? ;)

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

回答by Thomas Bonini

Dan Story and ergosys provided good answers (apparently I was looking in the wrong place, that's why the headaches) but while I'm at it I wanted to provide a copy&paste solution for the function in my question for future googlers:

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

int GetCrc32(const string& my_string) {
    boost::crc_32_type result;
    result.process_bytes(my_string.data(), my_string.length());
    return result.checksum();
}

回答by Dan Story

You probably want to use the crc_32_typeinstead of using the crc template. The template is general and meant to accommodate a wide range of CRC designs using widely varying parameters, but they ship four built-in pre-configured CRC types for common usage, covering CRC16, CCITT, XMODEM and CRC32.

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

回答by ergosys

The library includes predefined CRC engines. I think the one you want is crc_32_type. See this example: http://www.boost.org/doc/libs/1_37_0/libs/crc/crc_example.cpp

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

回答by Marcelo Cantos

Have you tried using the predefined crc_32_type?

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

回答by Ben Voigt

On this page, find the particular 32-bit CRC you want, read off all the other parameters: http://regregex.bbcmicro.net/crc-catalogue.htm

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