42

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 Bonini
  • 44,018
  • 30
  • 122
  • 156
  • You can also use this http://svn.abisource.com/wv/branches/release-version-0-7-12/crc32.c and http://svn.abisource.com/wv/branches/release-version-0-7-12/crc32.h. I assume the algorithm is the same but the speed is way better than boost crc. – schoetbi Apr 21 '11 at 12:08

5 Answers5

77

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:

#include <boost/crc.hpp>

uint32_t GetCrc32(const string& my_string) {
    boost::crc_32_type result;
    result.process_bytes(my_string.data(), my_string.length());
    return result.checksum();
}
parsley72
  • 8,449
  • 8
  • 65
  • 98
Andreas Bonini
  • 44,018
  • 30
  • 122
  • 156
10

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
  • 9,985
  • 1
  • 23
  • 27
5

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
  • 47,835
  • 5
  • 49
  • 70
4

Have you tried using the predefined crc_32_type?

Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
4

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
  • 277,958
  • 43
  • 419
  • 720