#include <stdio.h>
#include <stdint.h>
uint16_t crc16(uint8_t *data, int length)
{
uint16_t crc = 0xFFFF;
for (int i = 0; i < length; i++)
{
crc ^= data[i];
for (int j = 0; j < 8; j++)
{
if (crc & 1)
{
crc >>= 1;
crc ^= 0xA001;
}
else
{
crc >>= 1;
}
}
}
return crc;
}
uint16_t calculateCRC(uint8_t *data, int length)
{
return crc16(data, length);
}
int main()
{
uint8_t message[] = { 0x47,0xC7 };
int length = sizeof(message) / sizeof(message[0]);
uint16_t crc = calculateCRC(message, length);
printf("CRC: 0x%04X\n", crc);
return 0;
}
crc校验
最新推荐文章于 2024-09-10 10:21:38 发布