以下是使用C语言计算crc32码的示例代码:

#include <stdio.h>
#include <stdint.h>

/* CRC-32 polynomial */
#define POLY 0xEDB88320

/* Calculate CRC-32 for a buffer */
uint32_t crc32(const void *buf, size_t size)
{
    uint32_t crc = ~0;
    const uint8_t *p = buf;

    for (size_t i = 0; i < size; i++) {
        crc ^= p[i];
        for (int j = 0; j < 8; j++) {
            crc = (crc >> 1) ^ (-(crc & 1) & POLY);
        }
    }

    return ~crc;
}

int main()
{
    char msg[] = "hello, world!";
    uint32_t crc = crc32(msg, sizeof(msg)-1);
    printf("CRC-32: %08x\n", crc);
    return 0;
}

该代码使用CRC-32多项式0xEDB88320计算给定缓冲区的CRC-32校验和。该函数返回计算出的CRC-32值。在主函数中,使用示例字符串“hello,world!”调用crc32函数,并打印CRC-32值。

注意,该代码使用了C99标准中的一些特性,例如变量声明和for循环中的变量初始化。如果编译器不支持这些特性,请将代码修改为符合您的编译器的语法

用C语言编写计算crc32码

原文地址: https://www.cveoy.top/t/topic/hhJe 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录