Created
November 7, 2014 11:17
-
-
Save Imater/d49fbb934f7ae028f2e9 to your computer and use it in GitHub Desktop.
Fletcher algorithm of controll summ
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
function calcFletcherSum(data) { | |
var length = data.length, i = 0, sum1 = 0xFF, sum2 = 0xFF; | |
while (length) { | |
var tlen = length > 21 ? 21 : length; | |
length -= tlen; | |
do { | |
var ch = data.charCodeAt(i++); | |
if (ch > 255) { | |
var ch2 = ch >> 8; | |
ch &= 0xFF; | |
ch ^= ch2; | |
} | |
sum1 += ch; | |
sum2 += sum1; | |
} while (--tlen); | |
sum1 = (sum1 & 0xFF) + (sum1 >> 8); | |
sum2 = (sum2 & 0xFF) + (sum2 >> 8); | |
} | |
var result = (((sum1 & 0xFF) + (sum1 >> 8)) << 8) | ((sum2 & 0xFF) + (sum2 >> 8)); | |
return result == 0xFFFF ? 0 : result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment