C语言如下:
uint32_t crc32_compute(uint8_t const * p_data, uint32_t size)
{
uint32_t crc;
crc = 0xFFFFFFFF;
for (uint32_t i = 0; i < size; i++)
{
crc = crc ^ p_data[i];
for (uint32_t j = 8; j > 0; j--)
{
crc = (crc >> 1) ^ (0xEDB88320U & ((crc & 1) ? 0xFFFFFFFF : 0));
}
}
return ~crc;
}
JAVA 代码如下:
public class CRC32 {
private static long[] crc32Table = new long[256];
static {
long crcValue;
for (int i = 0; i < 256; i++) {
crcValue = i;
for (int j = 0; j < 8; j++) {
if ((crcValue & 1) == 1) {
crcValue = crcValue >> 1;
crcValue = 0x00000000edb88320L ^ crcValue;
} else {
crcValue = crcValue >> 1;
}
}
crc32Table[i] = crcValue;
}
}
计算Bytes 里所有字节的CRC32
public static long calculate(byte[] bytes) {
long resultCrcValue = 0x00000000ffffffffL;
for (int i = 0; i < bytes.length; i++) {
int index = (int) ((resultCrcValue ^ bytes[i]) & 0xff);
resultCrcValue = crc32Table[index] ^ (resultCrcValue >> 8);
}
resultCrcValue = resultCrcValue ^ 0x00000000ffffffffL;
return resultCrcValue;
}
}