write a crc funtion based on C
时间: 2024-05-13 15:21:00 浏览: 20
Here is an implementation of a CRC function in C:
```c
#include <stdio.h>
#include <stdint.h>
uint32_t crc32(const void *data, size_t length)
{
const uint8_t *buf = (const uint8_t *)data;
uint32_t crc = 0xFFFFFFFF;
for (size_t i = 0; i < length; i++) {
crc ^= buf[i];
for (int j = 0; j < 8; j++) {
if (crc & 1) {
crc = (crc >> 1) ^ 0xEDB88320;
} else {
crc >>= 1;
}
}
}
return ~crc;
}
int main()
{
const char *str = "hello world";
uint32_t crc = crc32(str, strlen(str));
printf("CRC32 of \"%s\" is 0x%08x\n", str, crc);
return 0;
}
```
This implementation uses the CRC-32 algorithm with the polynomial 0xEDB88320. The input data is passed as a pointer to a buffer and its length. The function returns the computed CRC-32 value as a 32-bit integer. The main function shows an example usage of the function with a string input.
相关推荐
![exe](https://img-home.csdnimg.cn/images/20210720083343.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)