CRC 计算 C 语言例子

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

void uint32_2_bin(char *buf, uint32_t val ){
    uint32_t b = 0x80000000;
    while(b>0){
        if( val&b ) *buf = '1';
        else *buf = '0';
        b>>=1;
        buf++;
    }
    *buf = '\0';
}

/**
 * param:
 *      data -- 需要计算的数组
 *      data_len -- 计算数组的长度
 *      div_e -- mod2 的除数
 *          如果公式为 X^8 + X^5 + X^4 + X
 *          那么 div_e == 0x131
 *      init -- 初始值,如果没有就填写 0x00
 *
 * 注意该函数目前是按照 crc8 的方式编写,
 * 通过变更返回值类型和 div_e 等变量可以变更成 crc16 crc32
 */
uint8_t mod2_division_8bits( uint8_t data[], uint32_t data_len, uint32_t div_e, uint8_t init ){
    uint8_t *end = data + data_len;
    uint32_t a = *data^init;

    uint8_t div_bits;
    uint32_t d;

    uint8_t b = 8;
    uint8_t c = 0;
    uint8_t sub;

#if 1
    {
        int i;
        for(i=0; i<32; i++)
            if( div_e & (0x01<<i) )
                div_bits = i+1;
        d = 0x01<<(div_bits-1);
    }
#else
    // fomule = X^8 + X^5 + X^4 + X;
    d = 0x100;
    div_bits = 9;
#endif

    while(1){
        if( b < div_bits ){
            if( data != end ){
                if( c > 0 ){
                    uint8_t bit = sub&c?1:0;
                    a = (a<<1)|bit;
                    c >>= 1;
                    if( a&d ) b++;
                }else{
                    data++;
                    sub = *data;
                    c = 0x80;
                    continue;
                }
            }else{
                if( c == 0 )
                    break;
                a <<= 1;
                c >>= 1;
                if( a&d ) b++;
            }
        }else{
            if( a&d )
                a ^= div_e;

            char s[64];
            uint32_2_bin( s, a );
            printf( "%s %2x\n", s, a );
            b--;
        }
    }

    return a&0xFF;
}

int main(void){

    uint8_t a[] = { 28, 184, 245, 165, 156, 208 };
    uint16_t b = 0x131;

    mod2_division_8bits( a, 6, b, 0xFF );

    return 0;
}
posted @ 2024-09-17 13:31  Mojies  阅读(11)  评论(0编辑  收藏  举报