Python CRC Calculation Library
libscrc is a Python library designed for calculating various Cyclic Redundancy Check (CRC) values, including CRC3, CRC4, CRC8, CRC16, CRC24, CRC32, CRC64, and CRC82. The latest version is 1.8.1, released on August 18, 2022. While releases are not frequent, the project shows ongoing maintenance and issue tracking through its GitHub repository.
Common errors
-
ERROR: Failed building wheel for libscrc
cause The `libscrc` library is a C extension and requires a C compiler (like GCC or Clang) and Python development headers to compile if a pre-built wheel is not available for your system and Python version.fixInstall the necessary build tools for your operating system. For Debian/Ubuntu: `sudo apt-get install build-essential python3-dev`. For macOS: `xcode-select --install`. For Windows, install 'Desktop development with C++' from Visual Studio Installer. -
ModuleNotFoundError: No module named 'libscrc' (or ImportError)
cause This usually indicates that the `libscrc` package failed to install correctly, often due to compilation errors during the C extension build process, even if `pip install` seemed to finish.fixReview the output of `pip install libscrc` for any error messages, especially those related to building wheels or C compilation. Ensure a C compiler and Python development headers are installed, then try reinstalling.
Warnings
- breaking Installation may fail on systems without a C compiler or specific Python versions due to `libscrc` being a C extension. Pre-built wheels are not available for all Python versions and platforms.
- gotcha The API for 'gradually calculating functions' has seen revisions. It was introduced, reverted in v1.4 (2020-08-04) due to issues, and then updated again in v1.7 (2021-06-07).
- gotcha CRC algorithms have many variants (polynomial, initial value, XOR-out, reflection settings). Using the wrong `libscrc` function or incorrect parameters will result in an invalid CRC.
Install
-
pip install libscrc
Imports
- libscrc
import libscrc
Quickstart
import libscrc
# Calculate CRC16 (Modbus RTU)
data_modbus = b'\x01\x02\x03\x04'
crc_modbus = libscrc.modbus(data_modbus)
print(f"CRC16 (Modbus) for {data_modbus.hex()}: {crc_modbus:04X}")
# Calculate CRC8 (Intel Hex)
data_intel = b'1234'
crc_intel = libscrc.intel(data_intel)
print(f"CRC8 (Intel Hex) for {data_intel.decode()}: {crc_intel:02X}")
# Calculate CRC32 (File/WinRAR)
data_crc32 = b'example data'
crc_file = libscrc.crc32(data_crc32)
print(f"CRC32 (File) for '{data_crc32.decode()}': {crc_file:08X}")
# Gradual CRC calculation (e.g., XMODEM), supported since v1.4+
data_part1 = b'hello'
data_part2 = b'world'
initial_crc = libscrc.xmodem(data_part1)
final_crc = libscrc.xmodem(data_part2, initial_crc)
print(f"CRC16 (XMODEM) gradually: {final_crc:04X}")