Calculate Checksum Pattern

This design pattern calculates a single-byte basic checksum from an array of bytes.

 

Pattern Syntax

 

 

private byte CalculateChecksum( byte[] data )

{

   byte checksum = 0;

 

   foreach( byte b in data )

      checksum += b;

 

   return checksum;

}

 

 

Remarks

This design pattern:

 

Note

If the result of adding byte value to the checksum exceeds 255 the addition will automatically "roll over" to zero and add the remainder, e.g. 253 + 8 results in 5 rather than 261.

 

See Also

Checksums

Design Patterns Index