用C语言编写64位CRC校验程序代码
时间: 2024-05-19 14:13:39 浏览: 153
C语言CRC校验程序
以下是一个使用C语言编写的64位CRC校验程序代码:
#include <stdio.h>
#include <stdint.h>
#define POLY UINT64_C(0x42f0e1eba9ea3693)
uint64_t crc64(uint64_t crc, const void *buf, size_t size)
{
const uint8_t *p = buf;
crc = ~crc;
while (size--)
{
crc ^= (uint64_t)(*p++) << 56;
for (int i = 0; i < 8; i++)
{
if (crc & UINT64_C(0x8000000000000000))
{
crc = (crc << 1) ^ POLY;
}
else
{
crc <<= 1;
}
}
}
return ~crc;
}
int main()
{
const char *str = "Hello, world!";
uint64_t crc = crc64(UINT64_C(0), str, sizeof(str) - 1);
printf("%016llx\n", crc);
return 0;
}
阅读全文