CRC
calculator.
Get the CRC-8, CRC-16 or CRC-32 checksum of a message, with the bytes to send.
- Polynomial
- 0x04C11DB7
- Initial value
- 0xFFFFFFFF
- Reflected
- In and out
- Final XOR
- 0xFFFFFFFF
CRC-32 (Ethernet, ZIP, PNG) reads these 9 bytes as one long binary number and keeps the 32-bit remainder after dividing by the polynomial 0x04C11DB7. To send it after the data, write the bytes 26 39 F4 CB, low byte first, as this reflected model expects. A CRC catches accidental damage. Anyone can change the data and work out a new CRC, so use a hash or HMAC against tampering.
The register, byte by byte
Slide or use the arrow keysAfter byte 9 of 9 (39, 9) the register holds 0x9B63D02C, and the CRC of the bytes so far is 0xCBF43926.
Working
- 1Turn the text into bytes (UTF-8)31 32 33 34 35 36 37 38 39 (9 bytes)
- 2Load the register with the initial valueregister = 0xFFFFFFFF
- 3This model reads each byte low bit first, so reverse its bits31 = 00110001 → 10001100
- 4Feed in every bit: shift the register left, and XOR with 0x04C11DB7 when the bit pushed out differs from the message bitafter byte 1: 0x1208C43Eafter byte 2: 0x4CDD350D⋮after byte 9: 0x9B63D02C
- 5Reverse the register's bits for output0x9B63D02C → 0x340BC6D9
- 6XOR with the final value0x340BC6D9 XOR 0xFFFFFFFF = 0xCBF43926
- 7Check: append the CRC bytes 26 39 F4 CB and read the codeword again; the register should equal the catalog residue0xDEBB20E3 = 0xDEBB20E3
The CRC after each byte
| Byte | Value | CRC so far | Register |
|---|---|---|---|
| 1 | 31 (1) | 0x83DCEFB7Register 0x1208C43E | 0x1208C43E |
| 2 | 32 (2) | 0x4F5344CDRegister 0x4CDD350D | 0x4CDD350D |
| 3 | 33 (3) | 0x884863D2Register 0xB439EDEE | 0xB439EDEE |
| 4 | 34 (4) | 0x9BE3E0A3Register 0x3AF83826 | 0x3AF83826 |
| 5 | 35 (5) | 0xCBF53A1CRegister 0xC7A3502C | 0xC7A3502C |
| 6 | 36 (6) | 0x0972D361Register 0x7934B16F | 0x7934B16F |
| 7 | 37 (7) | 0x5003699FRegister 0x06693FF5 | 0x06693FF5 |
| 8 | 38 (8) | 0x9AE0DAAFRegister 0x0AA4F8A6 | 0x0AA4F8A6 |
| 9 | 39 (9) | 0xCBF43926Register 0x9B63D02C | 0x9B63D02C |
Which algorithm gives my CRC?
Every algorithm on these bytes
| Algorithm | CRC |
|---|---|
| CRC-8 (SMBus) | 0xF4 |
| CRC-16/ARC | 0xBB3D |
| CRC-16/MODBUS | 0x4B37 |
| CRC-16/CCITT-FALSE | 0x29B1 |
| CRC-16/XMODEM | 0x31C3 |
| CRC-32 | 0xCBF43926 |
| CRC-32C | 0xE3069283 |
A CRC for each line
Results
| Line | CRC-32 | Bytes |
|---|---|---|
| 123456789 | 0xCBF43926Bytes 9 | 9 |
| Hello, world | 0xE79AA9C2Bytes 12 | 12 |
| IEND | 0xAE426082Bytes 4 | 4 |
How it's worked out
A cyclic redundancy check treats your message as one long binary number. It divides that number by a fixed binary number, the generator polynomial, and keeps the remainder. The remainder is the CRC. The division uses mod-2 arithmetic, where adding and subtracting are both XOR, so there are no carries or borrows.
Ross Williams' guide works one through by hand. First append as many zeros as the CRC has bits. Then, under every leading 1, XOR in the generator. Here the message is 1101011011 and the generator is 10011, a 4-bit CRC, so four zeros go on the end and the remainder is 1110.
| Bit | Under the generator | After XOR |
|---|---|---|
| 1 | 11010 | 01001 |
| 2 | 10011 | 00000 |
| 7 | 10110 | 00101 |
| 9 | 10100 | 00111 |
Here is the message, is the CRC width in bits and is the generator. Real algorithms add four settings on top. The register starts at an initial value instead of 0. Each byte may be read low bit first, which is called reflected input. The result may be reflected too. Last, the result is XORed with a final value.
The CRC RevEng catalog names each set of settings as a model and gives its check value, the CRC of the text 123456789. That is why CRC-16/ARC and CRC-16/MODBUS share the polynomial 0x8005 yet give 0xBB3D and 0x4B37: only the initial value differs. The tool reads every bit with the same loop the catalog describes, and the working shows the register after each byte.
Worked examples
Priya builds a Modbus request by hand
Priya wants to read 10 registers from device 1 over a serial line. The request is the six bytes 01 03 00 00 00 0A: the address, the function code, the start register and the count.
CRC-16/MODBUS of those bytes is 0xCDC5. The Modbus serial line specification says the low byte goes first, so the frame on the wire ends with C5 CD.
A receiver can check the whole frame in one pass. Run all eight bytes through the same CRC and the register comes out at 0x0000, the residue the catalog lists for this model. Anything else means a byte was damaged or the CRC bytes were swapped.
Tom tests his own CRC-32 code
Tom wrote a CRC-32 routine for a file format and wants to know if it is right. He feeds it the nine bytes of the text 123456789. The catalog check value is 0xCBF43926, and the tool gives 0xCBF43926.
His routine gives a different answer, so he compares the CRC after each byte. After the first byte, the character 1, it should be 0x83DCEFB7. After two bytes it should be 0x4F5344CD. The first byte where his value differs tells him where the bug starts.
Once the values agree, he appends the CRC, low byte first, and runs the codeword through. The register should read 0xDEBB20E3, which is the CRC-32 residue in the catalog, before the final XOR.
Ana works out which CRC-16 a sensor uses
Ana's sensor manual says only "CRC-16". She sends it 123456789 and it answers 0xBB3D. On those bytes, CRC-16/MODBUS gives 0x4B37, CRC-16/XMODEM gives 0x31C3 and CRC-16/CCITT-FALSE gives 0x29B1.
She types BB3D as the CRC she expects, and the search finds one match: CRC-16/ARC. That model starts at 0 and reads bits low first. Now her code can use the same settings.
Reading your result
The value. The answer is shown in hex, the way datasheets print it, with the decimal value under it. A 16-bit CRC is two bytes and a 32-bit CRC is four. The bytes to send come next. Reflected models usually send the low byte first, and the others send the high byte first.
The codeword check. Append the CRC to the data in that order and work out the register again. If the data arrived intact, you get a fixed number called the residue. For CRC-32 it is 0xDEBB20E3, and for the CRC-16 models here it is 0. The working ends with this check.
What it catches. Williams notes that a generator with at least two 1 bits catches every single flipped bit. For longer bursts of damage he cites Tanenbaum: the chance one slips through is about 1 in 2 to the power of W. That is about 1 in 4,294,967,296 for CRC-32.
What it does not do. A CRC is not secret. Anyone who changes the data can work out a new CRC that matches, so it does not prove the data came from you. For that, use a hash with a key, such as HMAC-SHA-256.
Common mistakes
- A line ending sneaks in. CRC-32 of "Hello, world" is 0xE79AA9C2. With a newline on the end it is 0x475A3FA6, and with a Windows line ending it is 0x4264CCB6. Files and terminals often add one.
- Text where you meant hex. Typed as hex, 31 32 33 gives 0x884863D2, the same as the text 123. Typed as text, the same eight characters are eight bytes, spaces included, and give 0xA88D909D.
- The CRC bytes swapped. A Modbus CRC of 0xCDC5 goes on the wire as C5 CD. Sending the high byte first gives a frame that fails the check.
- Trusting the name alone. "CRC-CCITT" means different things in different code. The catalog says the model often given that name is really CRC-16/IBM-3740, also called CCITT-FALSE, with check value 0x29B1. CRC-32C gives 0xE3069283 on the check string, not 0xCBF43926.