Skip to content
EN
  • Español
  • English
Advertisement

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.

Fill in the values you have and the rest are calculated. You can type in any of them.

In hex, pairs of digits with or without spaces (01 03 00 00 00 0A); as text, as it is.
—
Type the CRC you received to check it.
HEX
HEX
CRC correct? (1 yes, 0 no)—
CRC in decimal—
Message bytes—
Syndrome: position of the wrong bit (0 = no error)—
Parity bits—
Word length—
Code rate (data / word)—

Use this calculator for reference only, as we cannot guarantee that the results are free of errors.

Disclaimer

https://dyaii.es/en/crc-hamming-code-calculator/

Advertisement

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

CRC = remainder of M(x) · xⁿ / G(x)   (binary division without carries, using XOR)

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:

CRCPolynomialInitialReflectedFinal XOR"123456789"Where
CRC-16/MODBUS8005FFFFyes00004B37Modbus RTU
CRC-16/CCITT-FALSE1021FFFFno000029B1Cards, device protocols
CRC-16/XMODEM10210000no000031C3File transfer, Bluetooth
CRC-3204C11DB7FFFFFFFFyesFFFFFFFFCBF43926Ethernet, ZIP, PNG
CRC-8/SMBUS0700no00F4SMBus, 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.

Advertisement

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).

2r ≥ k + r + 1

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.

Advertisement

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.

Related calculators

Share

WhatsApp X LinkedIn Telegram Email
Advertisement

Install Diseño y automatización de instalaciones industriales on your phone

All the calculators one tap away, full screen, like an app. It takes no space and is always up to date.