用c语言写一段CRC32的代码
时间: 2024-05-02 10:19:47 浏览: 175
好的,以下是一段基于C语言的CRC32计算代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
uint32_t crc32(const void *data, size_t n_bytes){
const uint8_t *bytes = (const uint8_t *)data;
uint32_t crc = 0xffffffff;
for(size_t i = 0; i < n_bytes; i++){
uint8_t b = bytes[i];
for(int j = 0; j < 8; j++){
uint32_t mask = -(crc & 1);
crc = (crc >> 1) ^ (0xedb88320 & mask);
crc ^= (b >> j) & 1 ? 0xffffffff : 0;
}
}
return ~crc;
}
int main(){
char *data = "Hello, world!";
size_t n_bytes = strlen(data);
uint32_t result = crc32(data, n_bytes);
printf("CRC32: %08x\n", result);
return 0;
}
```
希望对您有所帮助!
阅读全文