#include <stdio.h> #include <stdint.h> // CRC-16-CCITT标准的多项式 #define POLY 0x1021 uint16_t crc16(uint16_t crc, const unsigned char *buffer, size_t len) { while (len--) { crc ^= (*buffer++ << 8); for (int i = 0; i < 8; i++) { if (crc & 0x8000) crc = (crc << 1) ^ POLY; else crc = crc << 1; } } return crc; } int main() { // 示例数据 unsigned char data[] = "Hello, World!"; size_t data_len = sizeof(data) - 1; // 不包括末尾的 '\0' // 初始化CRC值 uint16_t crc = 0xFFFF; // 通常初始值为0xFFFF // 计算CRC16校验和 crc = crc16(crc, data, data_len); // 打印结果 printf("CRC16: 0x%04X\n", crc); return 0; }
标签:crc16,buffer,len,uint16,版本,linux,include From: https://www.cnblogs.com/hshy/p/18262897