On Apr 13, 4:54 pm, Kevin O <[email protected]> wrote:
> I have an ASCII string of *09sw13100* that is supposed to result in hex *B8.
> *That is what I am using as my test.
>
> Any ideas on how to implement this? I've tried the function below but it
> produces the wrong output.
>
> [...]
Try this:
function calcChecksum(str) {
var sum = 0, i = 0, len = str.length;
for (; i<len; ++i) {
sum += str.charCodeAt(i);
if (sum & 0x80)
sum = (0xff - sum + 1) * -1;
}
sum = sum % 256;
if (sum < 0)
sum *= -1;
return sum.toString(16);
}
console.log(calcChecksum('09sw13100'));
// output: b8
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en