faq

General questions

Why yet another CRC tool?

Because I couldn't find any program which met my needs. I wanted an Open Source tool with the following features:

Over the years zillions of different CRC code snippets have accumulated in the Web, many of them are not compatible one with another, even if they are called the same (let's say CRC 16). pycrc tries to bring some clarity into this mess, by religiously using the Rocksoft Model, described by Ross Williams in Painless Guide to CRC Error Detection Algorithms.

How can I find out the parameters of a CRC?

It happens often that one has a few bytes of sample data and the CRC but a few parameters are not given. How can the missing CRC parameters be found?

Gregory Ewing wrote up an article on his experience of Reverse-Engineering a CRC Algorithm. It outlines an algorithm how to get the CRC parameters from raw data. Greg Cook's Perl script CRC RevEng does a good job in finding the parameters when a few data points are given.

Python implementation

What version of Python is supported?

pycrc requires Python 2.6 or later. Python 3.x is supported. The last version compatible with Python 2.4 is pycrc v0.7.10.

I get an error: "No such file or directory" when trying to run pycrc.py on Linux

This happens if the file pycrc.py has MS-DOS line endings. This is the case if you download the ZIP archive. You can either download the tar.gz version from the download page or convert the file to UNIX file ending:

dos2unix /path/to/pycrc.py

How do I calculate a CRC from python?

It is very straightforward to use the CRC implementation in python.

import binascii
import pycrc.algorithms

crc = pycrc.algorithms.Crc(width = 16, poly = 0x8005,
          reflect_in = True, xor_in = 0xffff,
          reflect_out = True, xor_out = 0x0000)

data = binascii.a2b_hex(b'01020304A12B')    # your data is probably already in binary form, so you won't have to convert it again.
my_crc = crc.bit_by_bit_fast(data)          # calculate the CRC, using the bit-by-bit-fast algorithm.
print('{:#04x}'.format(my_crc))

But the Python implementation is extremely inefficient!

This is correct. And I'm not going to change this. The Python implementation is thought to provide a correct, stable and easy to read code bases for further experiments. The right place to do performance tuning is the generated C code. I'm happy to consider every suggestion to improve the generated code.

CRC source generator

What is the best model?

This depends. For occasional computations on small amounts of data the optimised bit-by-bit implementation might be a feasible solution and might also have a speed which is comparable to other models. On desktop computers and with larger amounts of data, the table driven model is a good choice, whereas on embedded platforms where code space is a major concern, the bit-by-bit-fast implementation might be the better choice.

ParametersWidthPlatformData quantityPossible algorithms
Variable, Fixed1-16 bitsEmbedded, DesktopLowbit-by-bit, bit-by-bit-fast
Fixed8, 16 bitsEmbedded, DesktopMedium, Lowbit-by-bit, bit-by-bit-fast, table-driven (table index width: 4)
Fixed8 or more bitsDesktop, ServerMedium, Hightable-driven (possibly with slice-by 4, 8 or 16)

How fast are the algorithms?

The following numbers are collected using an unspecified model on a unspecified computer using an unspecified compiler (at maximum optimisation level). The numbers are only meaningful to get a rough idea how the algorithms compare against each other in terms of performance.

AlgorithmBlock sizeThroughput
bit-by-bit1024 bytes125 MiB/s
bit-by-bit-fast1024 bytes150 MiB/s
table-driven (table index width: 4)1024 bytes250 MiB/s
table-driven1024 bytes500 MiB/s
table-driven (slice-by: 4)1024 bytes1300 MiB/s
table-driven (slice-by: 16)1024 bytes4200 MiB/s

Can pycrc generate C++ code?

Yes and no. pycrc generates code that can be compiled by a C++ compiler, but does not generate classes or use any C++ feature.

Who owns the copyright of the generated code?

The sloppy answer is: you are free to do whatever you like with the generated code, as long as you don't blame the author for any malfunction or damage caused by the program.

However, as a courtesy, please keep the line that states that the code was generated by pycrc.