Skip to content

Instantly share code, notes, and snippets.

@guileen
Created January 7, 2014 07:44
Show Gist options
  • Save guileen/8295911 to your computer and use it in GitHub Desktop.
Save guileen/8295911 to your computer and use it in GitHub Desktop.
crc16
exports.crc16 = function (buffer, len) {
var crc = 0xffff;
for (var i = 0; i < len; i++) {
crc = ((crc ^ buffer[i]) & 0x00ff) + (crc & 0xff00);
for (var bit = 0; bit < 8; bit++) {
if ((crc & 0x0001) === 0x0000) {
crc >>= 1;
}
else {
crc >>= 1;
crc ^= 0xa001;
}
}
}
crc = ((crc & 0x00ff) << 8) + ((crc & 0xff00) >> 8);
return(crc);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment