Easy CRC checksums with Boost

The Boost CRC library makes it painless to compute the CRC checksum on data.  For HDL designers, the library can be useful in generating test vectors for use in testbenches for CRC modules.  The Boost documentation provides a complicated example program that shows how to use the library, but I stripped it down to create an even simpler example of how to use do a CCITT CRC-16 checksum.

#include <boost/crc.hpp>
#include <boost/integer.hpp>
#include <cassert>

int main(int argc, char *argv[])
{
  unsigned char const test_data[] = { 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39 };
  std::size_t const test_data_len = sizeof(test_data) /   sizeof(test_data[0]);
  boost::uint16_t const expected_result = 0x29B1;

  boost::crc_ccitt_type checksum_agent;
  checksum_agent.process_bytes(test_data, test_data_len);

  assert(checksum_agent.checksum() == expected_result);
  return 0;
}
Advertisements

One thought on “Easy CRC checksums with Boost

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s