{"id":1677,"date":"2015-12-12T00:01:54","date_gmt":"2015-12-12T08:01:54","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1677"},"modified":"2015-12-15T08:37:14","modified_gmt":"2015-12-15T16:37:14","slug":"check-that-sum","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1677","title":{"rendered":"Check That Sum"},"content":{"rendered":"<p>The old computer processing adage is &#8220;garbage in, garbage out.&#8221; What it means is that unless your data is good, don&#8217;t expect to see good results. So what can be done to ensure that the data is good? A checksum, that&#8217;s what.<br \/>\n<!--more--><br \/>\nWhenever data is input, stored, or transmitted, good programming practice is to perform some type of check to confirm that the data desired is what&#8217;s input. You may perform such checks in your code already.<\/p>\n<p>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.<\/p>\n<p>Back in the old days, computer code appeared in books or magazines. The user would type in the data one line at a time.<\/p>\n<p>Don&#8217;t laugh: I did this type of typing exercise frequently. A lot of early hobbyists did.<\/p>\n<p>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.<\/p>\n<p>The following code presents three sample strings, <code>packet_a<\/code>, <code>packet_b<\/code>, and <code>packet_c<\/code>. Each string is displayed, followed by a simple checksum calculation, which the <em>checksum()<\/em> function calculates.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint checksum(char *line)\r\n{\r\n    int c = 0;\r\n\r\n    while(*line)\r\n    {\r\n        if( *line &gt;= 'A' && *line &lt;= 'F')\r\n            c += *line - 'A';\r\n        else\r\n            c += *line - '0';\r\n        line++;\r\n    }\r\n    return( c & 0xFF);\r\n}\r\n\r\nint main()\r\n{\r\n    char *packet_a = \"F6A4C8D4EF44D4AA91165C8E\";\r\n    char *packet_b = \"E6A4C8D4EF44D4AA91165C8E\";\r\n    char *packet_c = \"6EA4C8D4EF44D4AA91165C8E\";\r\n\r\n    printf(\"%-24s %s\\n\",\"Packet\",\"Checksum\");\r\n    printf(\"%24s  %X\\n\",packet_a,checksum(packet_a));\r\n    printf(\"%24s  %X\\n\",packet_b,checksum(packet_b));\r\n    printf(\"%24s  %X\\n\",packet_c,checksum(packet_c));\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>checksum()<\/em> 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 <code>c<\/code>, creating a cumulative sum.<\/p>\n<p>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.<\/p>\n<p>Here&#8217;s the output:<\/p>\n<pre><code>Packet                   Checksum\r\nF6A4C8D4EF44D4AA91165C8E  5C\r\nE6A4C8D4EF44D4AA91165C8E  5B\r\n6EA4C8D4EF44D4AA91165C8E  5B<\/code><\/pre>\n<p>The string appears, followed by the checksum.<\/p>\n<p>Both <code>packet_c<\/code> and <code>packet_b<\/code> share the same the checksum value. That shouldn&#8217;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.<\/p>\n<p>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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Calculate a checksum to ensure the quality of data. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1677\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-1677","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1677","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1677"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1677\/revisions"}],"predecessor-version":[{"id":1694,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1677\/revisions\/1694"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1677"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1677"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1677"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}