How to use boost::crc?

27,703

Solution 1

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:

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

Solution 2

You probably want to use the crc_32_type instead 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.

Solution 3

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

Solution 4

Have you tried using the predefined crc_32_type?

Solution 5

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

Share:
27,703
Andreas Bonini
Author by

Andreas Bonini

Updated on September 15, 2021

Comments

  • Andreas Bonini
    Andreas Bonini over 2 years

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

    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());
    }
    

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

  • Andreas Florath
    Andreas Florath over 2 years
    IMHO the return type should be unsigned (e.g. std::uint32_t).
  • Andreas Bonini
    Andreas Bonini over 2 years
    @AndreasFlorath, I completely agree! Thanks for pointing that out. I updated the answer to use it.