The old computer processing adage is “garbage in, garbage out.” What it means is that unless your data is good, don’t expect to see good results. So what can be done to ensure that the data is good? A checksum, that’s what.
Whenever data is input, stored, or transmitted, good programming practice is to perform some type of check to confirm that the data desired is what’s input. You may perform such checks in your code already.
For example, if a value is requested, the code confirms that the text input appears to be some form of value and not random characters.
Back in the old days, computer code appeared in books or magazines. The user would type in the data one line at a time.
Don’t laugh: I did this type of typing exercise frequently. A lot of early hobbyists did.
To confirm that you typed in the correct values, a checksum was included. The checksum was often a two-digit value that appeared at the end of a line of text. When the program processed the input, it calculated a checksum. If the value calculated matched what you typed, the assumption was that the data was good. Otherwise the line was flagged for input a second time.
The following code presents three sample strings, packet_a
, packet_b
, and packet_c
. Each string is displayed, followed by a simple checksum calculation, which the checksum() function calculates.
#include <stdio.h> int checksum(char *line) { int c = 0; while(*line) { if( *line >= 'A' && *line <= 'F') c += *line - 'A'; else c += *line - '0'; line++; } return( c & 0xFF); } int main() { char *packet_a = "F6A4C8D4EF44D4AA91165C8E"; char *packet_b = "E6A4C8D4EF44D4AA91165C8E"; char *packet_c = "6EA4C8D4EF44D4AA91165C8E"; printf("%-24s %s\n","Packet","Checksum"); printf("%24s %X\n",packet_a,checksum(packet_a)); printf("%24s %X\n",packet_b,checksum(packet_b)); printf("%24s %X\n",packet_c,checksum(packet_c)); return(0); }
The checksum() function at Line 3 chomps through the string one character at a time. Each character is converted to a value, with hex digits A through F translated to 10 through 15. The value of each digit is added to variable c
, creating a cumulative sum.
In Line 15, the logical AND operator lops off all the but final two digits of the cumulative sum, returning a truncated value. That value is the checkum.
Here’s the output:
Packet Checksum
F6A4C8D4EF44D4AA91165C8E 5C
E6A4C8D4EF44D4AA91165C8E 5B
6EA4C8D4EF44D4AA91165C8E 5B
The string appears, followed by the checksum.
Both packet_c
and packet_b
share the same the checksum value. That shouldn’t pose a problem, but it points out a weakness of the simple checksum calculation. If you accidentally swap values in the string, the checksum result is the same.
The Internet uses a more sophisticated form of error-checking for its information packets. That value is sent as part of the packet transmitted. When the receiving computer examines the data, it performs a complex calculation as a checksum. If the values are off, the packet is requested as second time.