Single-byte checksum of a payload
Many lightweight protocols use a one-byte checksum: just the sum of all bytes modulo 256.
- Each element in the payload is a byte value (0–255).
- The checksum is the sum of all bytes modulo 256.
- An empty payload has a checksum of 0.
ChecksumByte(payload: list<int>) → int
C# needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.
Where you start
public int ChecksumByte(List<int> payload) {
}
Worked examples
| Call | Result |
|---|---|
ChecksumByte(new List<int> { 1, 2, 3 }) | 6 |
ChecksumByte(new List<int> { 255, 1 }) | 0 |
ChecksumByte(new List<int> { }) | 0 |
ChecksumByte(new List<int> { 128, 128 }) | 0 |
Hint
Add every element, then take the remainder when divided by 256.
Reference solution in C#
public int ChecksumByte(List<int> payload) {
int sum = 0;
foreach (int b in payload) sum += b;
return sum % 256;
}