2-digit checksum: This is the hexadecimal two’s complement of the modulo-256 sum of the ASCII values of all characters in the message excluding the checksum itself and the CR-LF terminator at the end of the message. Permissible characters are ASCII 0-9 and upper case A-F. When all the characters are added to the Checksum, the value should equal 0.
--
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 nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
Well, you forgot to make it two's complement.
> Any ideas on how to implement this? I've tried the function below but it
> produces the wrong output.
>
> function calcChecksum(asciiString) {
> var buf = new Buffer(asciiString);
> var sum = 0;
> for(var i=0; i<buf.length; i++) {
> sum = sum + buf[i];
> }
> sum = sum%256;
Here, insert `sum = sum ^ 0xff`. That should work, I think.
> return sum.toString(16);
> }
function calcChecksum(asciiString) {
var sum= 0, i= asciiString.length;
while (i--) sum+= asciiString.charCodeAt(i);
sum= (256- (sum%= 256));
return (sum<16 ? '0' : '')+ sum.toString(16).toUpperCase();
}
calcChecksum('09sw13100')
-> "B8"
--
Jorge.
I am working on a node integration to a home automation device. The device has an ASCII protocol that I am building a parser and a command generator for.
Yeah, cool, that's the best !
function calcChecksum(asciiString) {
var sum= 0;
var i= asciiString.length;
while (i--) sum-= asciiString.charCodeAt(i);
return ((sum&= 0xff) < 16 ? '0' : '')+ sum.toString(16).toUpperCase();