#include <stdio.h>
#include <stdint.h>
typedef uint8_t BYTE;
typedef uint16_t WORD;
typedef uint32_t DWORD;
BYTE data[13] = { 0x84, 0x12, 0xF7, 0x64, 0x18, 0x02, 0x05, 0x00, 0xBB, 0x32, 0xE7, 0x64, 0x08 };
WORD calc_crc16_byte(WORD crc, BYTE data)
{
WORD temp = data;
crc = crc ^ (temp * 256);
DWORD bitIndex = 8;
do
{
DWORD carry = (crc >= 0x8000);
crc = (crc * 2) & 0xffff;
if (carry)
{ crc = crc ^ 0x1021; }
bitIndex = bitIndex - 1;
}
while (bitIndex > 0);
return crc;
}
WORD calc_crc16(void *data, DWORD length)
{
WORD crc = 0xFFFF;
BYTE *bytes = (uint8_t *)data;
for (uint32_t i = 0; i < length; i++)
{ crc = calc_crc16_byte(crc, bytes[i]); }
return crc;
}
int main()
{
WORD crc = calc_crc16(data, 13);
printf("CRC16: 0x%x\n", crc); //Should be 0xB81B
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: