Created
January 7, 2014 07:44
-
-
Save guileen/8295911 to your computer and use it in GitHub Desktop.
crc16
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
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