crc16

Version 1.2 (32.1 KB) by Mark
Table-based CRC-16
99 Downloads
Updated 10 Feb 2024

Table-Driven Implementation of CRC-16

CRC16 is a very readable implementation of CRC-16 which is compatible with MATLAB and GNU Octave. The algorithm is parameterized to support a large number of CRC-16 variants. The following parameters can be specified.

  • poly - CRC polynomial specified as a 16-bit integer (default=0x1021)
  • init - Initial shift register value specified as a 16-bit integer (default=0)
  • refin - Reflect (bit-reverse) input bytes, specified as a boolean (default=false)
  • refout - Reflect output CRC, specified as a boolean (default=false)
  • xorout - Value to XOR with the final CRC before returning, specified as a 16-bit integer (default=0)

The implementation uses only primitive functions without any dependencies on MATLAB toolboxes or GNU Octave packages. Because of this it will likely run on most any version. It has been tested with MATLAB versions R2019b, R2020b, and R2022b as well as GNU Octave versions 3.8.2, 6.4.2, and 8.3.0.

Files

  • crc16.m - Table-based CRC-16 algorithm; see the file's embedded help for customizing parameters and usage examples.
  • test_crc16.m - Test harness which verifies the algorithm against Greg Cook's Catalogue of parametrised CRC algorithms with 16 bits.

Transcript of test_crc16.m

Examples

1. CRC16 using default parameters corresponding to CRC-16/XMODEM

crc = crc16(uint8('123456789'));
disp(dec2hex(crc))
31C3

2. CRC-16/KERMIT, often identified as CRC-16/CCITT

crc = crc16(uint8('123456789'), struct('refin',1,'refout',1));
disp(dec2hex(crc))
2189

3. CRC-16/MODBUS

cfg = struct('poly',0x8005,'init',0xffff,'refin',1,'refout',1,'xorout',0);
crc = crc16(uint8('123456789'),cfg);
disp(dec2hex(crc))
4B37

References

  1. Greg Cook's CRC-16 Catalog
  2. Dr. Dobb's Understanding CRCs
  3. Sunshine Online CRC Calculator
  4. Online CRC-8 CRC-16 CRC-32 Calculator

Written with StackEdit.

Cite As

Mark (2025). crc16 (https://github.com/markagold1/crc16/releases/tag/v1.2), GitHub. Retrieved .

MATLAB Release Compatibility
Created with R2023b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
1.2

See release notes for this release on GitHub: https://github.com/markagold1/crc16/releases/tag/v1.2

1.1

See release notes for this release on GitHub: https://github.com/markagold1/crc16/releases/tag/v1.1

1.0

To view or report issues in this GitHub add-on, visit the GitHub Repository.
To view or report issues in this GitHub add-on, visit the GitHub Repository.