#include <stdio.h>

unsigned char crc(unsigned char *data, int len, unsigned char poly, unsigned char init)
{
    unsigned char crc = init;
    int i, j;

    for (i = 0; i < len; i++) {
        crc ^= data[i];
        for (j = 0; j < 8; j++) {
            if (crc & 0x80) {
                crc = (crc << 1) ^ poly;
            } else {
                crc <<= 1;
            }
        }
    }

    return crc;
}

int main()
{
    unsigned char data[] = {0x11, 0x22, 0x33, 0x44};
    int len = sizeof(data) / sizeof(data[0]);
    unsigned char poly = 0x21;
    unsigned char init = 0x0f;

    unsigned char result = crc(data, len, poly, init);

    printf("Result: 0x%02X\n", result);

    return 0;
}

该程序实现了CRC校验算法,校验宽度为6,多项式为0x21,初始值为0x0f。程序首先定义了一个名为crc的函数,该函数接收数据、数据长度、多项式和初始值作为参数,并返回计算得到的CRC校验值。

main函数中,程序首先定义了一个测试数据数组,然后调用crc函数计算CRC校验值,最后将结果打印到控制台。

该程序可以帮助用户理解CRC校验算法的实现原理,并方便用户根据需要进行修改和应用。

C语言CRC校验算法实现:宽度为6,多项式为0x21,初始值为0x0f

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

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