Skip to content

Instantly share code, notes, and snippets.

@tijnkooijmans
Created April 17, 2014 12:53
Show Gist options
  • Save tijnkooijmans/10981093 to your computer and use it in GitHub Desktop.
Save tijnkooijmans/10981093 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;
}
@jabelr
Copy link

jabelr commented Apr 3, 2025

vb.NET version

Private Function getCRC(Buffer() As Byte) As Byte()
    Dim crc As UInt16 = &HFFFF
    For i As Integer = 0 To Buffer.Length - 1
        crc = crc Xor (CInt(Buffer(i)) << 8)
        For j As Integer = 0 To 7
            If (crc And &H8000) > 0 Then
                crc = ((crc << 1) Xor &H1021)
            Else
                crc <<= 1
            End If
        Next
    Next
    Return BitConverter.GetBytes(crc)
End Function

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