This design pattern calculates a single-byte basic checksum from an array of bytes.
private byte CalculateChecksum( byte[] data )
{
byte checksum = 0;
foreach( byte b in data )
checksum += b;
return checksum;
}
This design pattern:
declares a private function that accepts a byte array parameter called data,
declares and initializes a byte variable called checksum,
iterates through the data array adding the value of each byte to the checksum variable,
returns the value of checksum.
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.