Created
May 12, 2012 12:47
-
-
Save jlamothe/2666368 to your computer and use it in GitHub Desktop.
CRC16 checksum calculator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdint.h> | |
#define CRC16 0x8005 | |
uint16_t gen_crc16(const uint8_t *data, uint16_t size) | |
{ | |
uint16_t out = 0; | |
int bits_read = 0, bit_flag; | |
/* Sanity check: */ | |
if(data == NULL) | |
return 0; | |
while(size > 0) | |
{ | |
bit_flag = out >> 15; | |
/* Get next bit: */ | |
out <<= 1; | |
out |= (*data >> (7 - bits_read)) & 1; | |
/* Increment bit counter: */ | |
bits_read++; | |
if(bits_read > 7) | |
{ | |
bits_read = 0; | |
data++; | |
size--; | |
} | |
/* Cycle check: */ | |
if(bit_flag) | |
out ^= CRC16; | |
} | |
return out; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This incorrect code was the subject of a question on Stackoverflow: https://stackoverflow.com/questions/10564491/function-to-calculate-a-crc16-checksum