Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use boost::crc?

Tags:

c++

boost

crc

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

like image 543
Thomas Bonini Avatar asked Apr 04 '10 06:04

Thomas Bonini


4 Answers

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();
}
like image 172
Thomas Bonini Avatar answered Sep 22 '22 21:09

Thomas Bonini


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.

like image 42
Dan Story Avatar answered Sep 19 '22 21:09

Dan Story


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

like image 29
ergosys Avatar answered Sep 19 '22 21:09

ergosys


Have you tried using the predefined crc_32_type?

like image 36
Marcelo Cantos Avatar answered Sep 22 '22 21:09

Marcelo Cantos