用C语言写一个CRC16查表法的程序
时间: 2023-11-12 14:06:53 浏览: 42
以下是一个简单的CRC16查表法的程序,使用了多项式0x8005和初始值0xFFFF:
```c
#include <stdio.h>
#include <stdint.h>
#define POLY 0x8005
#define INIT 0xFFFF
uint16_t crc16(uint8_t *data, uint16_t length) {
uint16_t crc = INIT;
for (int i = 0; i < length; i++) {
crc ^= data[i];
for (int j = 0; j < 8; j++) {
if (crc & 0x0001) {
crc = (crc >> 1) ^ POLY;
} else {
crc >>= 1;
}
}
}
return crc;
}
int main() {
uint8_t data[] = {0x01, 0x02, 0x03, 0x04, 0x05};
uint16_t crc = crc16(data, sizeof(data));
printf("CRC16: 0x%04X\n", crc);
return 0;
}
```
注意,这个程序中没有使用预先计算好的查表,而是在运行时动态计算CRC值。如果需要更高的性能,可以使用预先计算好的查表。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)