CRC and Hamming code calculator
Work out the CRC of a message (Modbus, CCITT, XMODEM, CRC-32, CRC-8) and check a received one, or encode data in Hamming and correct the wrong bit of a word.
When a PLC, a drive or a sensor sends data over a cable or radio, some bit may arrive flipped by noise. To notice it, the sender appends a few bits calculated from the message and the receiver repeats the sum: if it does not match, the message is wrong. The CRC (cyclic redundancy check) detects errors and is used in Modbus, Profibus, Ethernet, CAN or ZIP files. The Hamming code goes further: with a few extra bits it locates a wrong bit and corrects it, as in ECC memory.
How a CRC is calculated
The message bits are treated as a polynomial M(x) and divided by the generator polynomial G(x) of degree n; the n-bit remainder is the CRC. In practice it is done with a register that shifts bit by bit and XORs with the polynomial whenever a 1 comes out. Besides the polynomial, each CRC defines the initial register value, whether bits go in and out reflected (least significant bit first) and a final XOR:
| CRC | Polynomial | Initial | Reflected | Final XOR | "123456789" | Where |
|---|---|---|---|---|---|---|
| CRC-16/MODBUS | 8005 | FFFF | yes | 0000 | 4B37 | Modbus RTU |
| CRC-16/CCITT-FALSE | 1021 | FFFF | no | 0000 | 29B1 | Cards, device protocols |
| CRC-16/XMODEM | 1021 | 0000 | no | 0000 | 31C3 | File transfer, Bluetooth |
| CRC-32 | 04C11DB7 | FFFFFFFF | yes | FFFFFFFF | CBF43926 | Ethernet, ZIP, PNG |
| CRC-8/SMBUS | 07 | 00 | no | 00 | F4 | SMBus, sensors |
The last column is each one's check value for the text "123456789": use it to test your own implementation.
The Modbus RTU CRC
It is calculated over all the bytes of the frame (address, function and data) and sent low byte first. Reading 10 registers from 0 on slave 1 is 01 03 00 00 00 0A; its CRC is 16#CDC5 and the full frame 01 03 00 00 00 0A C5 CD. A typical mistake when coding it by hand is sending CD C5: the slave drops the frame without answering.
Hamming code
Parity bits go in positions 1, 2, 4, 8… of the word and data bits in the rest. Each parity p makes even the number of ones in the positions whose binary number contains p: p1 covers the odd ones; p2, 2, 3, 6, 7…; p4, 4 to 7, and so on. On reception they are recalculated: the failing ones, read in binary, give the position of the wrong bit (the syndrome).
With k data bits you need r parity bits: 3 for 4 bits, the classic Hamming (7,4); 4 for 11 (15,11); 6 for 32 and 7 for 64. Example: data 1011 give the word 0110011. If 0110111 arrives, parities 1 and 4 fail, 1 + 4 = 5: bit 5 is wrong, it is flipped and the data 1011 come back.
Hamming corrects one bit, but with two wrong bits it gets it wrong. Adding an overall parity (SECDED, used in ECC memory) corrects one and detects two.
How to use the calculator
For a CRC, choose the type, type the message in hex or as text and you get the CRC and its bytes in sending order. To check a received one, type it in the CRC box. For Hamming, type the data and you get the word; or type the received word and leave the data empty to see whether a bit is wrong and correct it.
Frequently asked questions
How is the CRC of a Modbus RTU frame calculated?
With CRC-16/MODBUS (polynomial 8005 reflected, A001, initial value FFFF) over all the bytes of the frame. For 01 03 00 00 00 0A it is 16#CDC5, sent low byte first: the full frame is 01 03 00 00 00 0A C5 CD.
How do I check the CRC I received?
Type the message without the CRC and the received CRC in its box: the calculator tells you whether it matches. Mind the byte order: in Modbus the low byte comes first, so C5 CD in the frame is CRC CDC5.
How does the Hamming code work?
It adds parity bits in positions 1, 2, 4, 8… and each one covers the positions whose binary number contains it. If a bit arrives flipped, the failing parities form its position in binary (the syndrome) and it is corrected. Data 1011 give the word 0110011.
How many parity bits does Hamming need?
The r that meet 2^r ≥ k + r + 1, k being the data bits: 3 for 4 bits (7,4), 4 for 11 (15,11), 6 for 32 and 7 for 64. It corrects one bit; with one more overall parity bit (SECDED) it also detects two.
