tags:

views:

442

answers:

5

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? ;)

+1  A: 

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.

Dan Story
+3  A: 

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

ergosys
+1  A: 

Have you tried using the predefined crc_32_type?

Marcelo Cantos
+2  A: 

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

Ben Voigt
+2  A: 

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:

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

Easy, huh? :)

Andreas Bonini