C语言实现CRC32算法

本文将使用C语言完成CRC32算法的实现;

C语言crc32算法

#include <inttypes.h>
#include <stdio.h>

uint32_t crc32(const char* s){

    uint32_t crc = 0xffffffff;
    size_t i = 0;
    while (s[i] != '\0')
    {
        uint8_t byte = s[i];
        crc = crc ^ byte;
        for (uint8_t j = 8; j > 0; --j)
        {
            crc = (crc >> 1) ^ (0xEDB88320 & (-(crc & 1)));
        }

        i++;
    }
    return crc ^ 0xffffffff;
}

int main(){
    printf("%" PRIu32 "\n", crc32("hello world"));//222957957
    printf("%" PRIx32 "\n", crc32("hello world"));//d4a1185
    return 0;
}

运行效果

对字符串"hello world"进行CRC32运算的结果如下:

222957957
d4a1185

原创内容,如需转载,请注明出处;

本文地址: https://www.perfcode.com/p/c-crc32.html

分类: 计算机技术
推荐阅读:
C语言中 i++ 和 ++i 的区别 在C语言中,++ 运算符也叫递增运算符,只需要一个操作数,属于一元运算符;本文将讨论前缀++运算符和后缀++运算符的区别,以及符号优先级的问题;
Golang通过使用GetSystemMetrics获取系统的分辨率 本文将使用Go语言调用GetSystemMetrics()函数来获取系统的分辨率。
TypeError: __format__ must return a str, not NoneType 在 Python 中,如__format__()方法必须返回一个字符串,否则将触发类似TypeError: __format__ must return a str, not NoneType的错误;
Rust使用莱布尼茨公式计算圆周率 莱布尼茨公式是一种用于计算圆周率的无限级数。该公式的形式如下:pi/4 = 1 - 1/3 + 1/5 - 1/7 + 1/9 - ... 通过对无限级数进行逐项求和,可以逐步逼近圆周率的值。在 Rust 中,可以使用循环和累加器来计算莱布尼茨级数的前N项和。
C语言中struct和typedef struct的用法和不同 在C语言中struct和typedef struct创建的结构体并没有什么不同,只是使用typedef后可以为结构体创建一个别名;
Python property()函数 property()函数是Python内置函数之一,用于创建属性。