Twos complement checksum
5 April 2008, 14:59 by Chad Reynoldson
Here is a netlinx function to calculate the twos complement checksum of a data packet. The BXOR statement is used to invert all of the bits, this provides the ones complement. Then you simply add 1 for the twos complement. This could all be done in one statement (cCS = (cCS BXOR $FF) + 1), but I broke it out so it was a bit more descriptive.
(*---------------------------------------------------------*)
(* Get the checksum as twos complement. *)
(*---------------------------------------------------------*)
DEFINE_FUNCTION CHAR getChecksum (CHAR cData[])
STACK_VAR
INTEGER nLoop
CHAR cCS
{
(*-- Add 'em up --*)
FOR(nLoop=1; nLoop<=LENGTH_STRING(cData); nLoop++)
cCS = cCS + cData[nLoop]
(*-- Get 1s Complement (invert all the bits) --*)
cCS = cCS BXOR $FF
(*-- Get 2s Complement (invert all the bits and add 1) --*)
cCS = cCS + 1
RETURN(cCS)
}
Comments
Comments are turned off for this article.
