Skip to content

Instantly share code, notes, and snippets.

@gnh1201
Forked from tijnkooijmans/crc16.c
Created January 22, 2021 08:40
Show Gist options
  • Save gnh1201/767041c12122a972703075e7b6126bfc to your computer and use it in GitHub Desktop.
Save gnh1201/767041c12122a972703075e7b6126bfc to your computer and use it in GitHub Desktop.
CRC-16/CCITT-FALSE
uint16_t crc16(char* pData, int length)
{
uint8_t i;
uint16_t wCrc = 0xffff;
while (length--) {
wCrc ^= *(unsigned char *)pData++ << 8;
for (i=0; i < 8; i++)
wCrc = wCrc & 0x8000 ? (wCrc << 1) ^ 0x1021 : wCrc << 1;
}
return wCrc & 0xffff;
}
@gnh1201
Copy link
Author

gnh1201 commented Jan 22, 2021

amindk17 commented on 25 Apr 2019
Python 3 version

def crc16(data : bytearray, offset , length):
    if data is None or offset < 0 or offset > len(data)- 1 and offset+length > len(data):
        return 0
    crc = 0xFFFF
    for i in range(0, length):
        crc ^= data[offset + i] << 8
        for j in range(0,8):
            if (crc & 0x8000) > 0:
                crc =(crc << 1) ^ 0x1021
            else:
                crc = crc << 1
    return crc & 0xFFFF

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment