CRC32 not calculated correctly
I am using a very simple algorithm to calculate CRC32, but it gives wrong values.
I am comparing my output values ββwith the calculator but it always looks different
unsigned int crc32_tab[256] = {
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
.........,..............,.............,.........,...........
};
A function using lookup table
unsigned int MyClass::crc32(unsigned int crc, const void *buf, unsigned int size)
{
const unsigned int *p;
p = (const quint8 *)buf;
crc = crc ^~ 0xFFFFFFFF;
while(size--)
{
crc = this->crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8);
}
return crc ^~ 0xFFFFFFFF;
}
I call it this way
QString test= QString::number(mclass.crc32(0, crcval, 6))
+3
source to share
1 answer
Solution taken from dicussion chat
CRC-32 Ethernet CRC-32 algorithm (generator polynomial 0x04C11DB7
).
This CRC-32 requires:
- Initialized with
0xFFFFFFFF
. - And completing XORing with
0xFFFFFFFF
.
Hence, you must remove the statements crc ^~ 0xFFFFFFFF
inside your function, pass 0xFFFFFFFF
in when you call the function, and once you are done CRCing the data, you must XOR the value with 0xFFFFFFFF
.
+2
source to share