{"id":5083,"date":"2021-12-08T00:01:06","date_gmt":"2021-12-08T08:01:06","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5083"},"modified":"2022-01-01T08:41:49","modified_gmt":"2022-01-01T16:41:49","slug":"encoding-a-string-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5083","title":{"rendered":"Encoding a String &#8211; Solution"},"content":{"rendered":"<p>The task for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5074\">this month&#8217;s Exercise<\/a> is to write an encoding filter that follows a specific pattern: After the first character (or byte) is output as a 2-digit hex value, the remaining characters are output as the difference between the previous character and the current character. I&#8217;m sure this type of encoding has an official name, but it&#8217;s the holidays and I&#8217;m too lazy to look it up.<br \/>\n<!--more--><br \/>\nThe encoder I created uses two <em>int<\/em> variables, <code>current<\/code> and <code>previous<\/code>. Variable <code>current<\/code> contains the current character read and <code>previous<\/code> contains the preceding character. This scheme is initialized with <code>previous<\/code> set equal to zero, so the first character read always retains its original value.<\/p>\n<p>Like any filter, I use a loop with the <em>getchar()<\/em> function as a condition to read standard input. The value read is output as a 2-digit hex value, the difference between <code>current<\/code> and <code>previous<\/code>. Then <code>previous<\/code> is assigned the value of <code>current<\/code>, and the loop continues:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2021_12-Exercise.c\" rel=\"noopener\" target=\"_blank\">2021_12-Exercise.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int current,previous;\r\n\r\n    <span class=\"comments\">\/* process input *\/<\/span>\r\n    chb = 0;\r\n    while( (current=getchar()) != EOF )\r\n    {\r\n        printf(\"%02X\",(unsigned char)(current-previous));\r\n        previous = current;\r\n    }\r\n    putchar('\\n');\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Both variables <code>current<\/code> and <code>previous<\/code> are declared as integers. This data type is the value returned from the <em>getchar()<\/em> function. The reason is that the EOF is an integer value, not a character. This value is tested for in the <em>while<\/em> loop condition, which is how the loop terminates.<\/p>\n<p>At Line 11, the <em>printf()<\/em> statement uses placeholder <code>%02X<\/code> to output a two-digit hexadecimal value, uppercase letters, with zero padded for single-digit output. This keeps the encoding consistent at two characters per byte.<\/p>\n<p>The <code>(unsigned char)<\/code> cast ensures that the value output is only two digits long. Otherwise, for negative values the compiler pay prefix an integer with a few <code>F<\/code> digits.<\/p>\n<p>At this point, you can play with the filter, but the data stays encrypted:<\/p>\n<p><code>$ .\/encode<br \/>\nWhat secrets are to be revealed?<br \/>\n5711F913AC53F2FE0FF30FFFAD4111F3BB54FBB14203BB52F311EFFC0BF9FFDBCB<\/code><\/p>\n<p>I hope that your solution met with success. An easy way to test is to process two adjacent letters, up and down:<\/p>\n<p><code>$ .\/encode<br \/>\nABA<br \/>\n4101FFC9<\/code><\/p>\n<p>The code for <code>'A'<\/code> is 41. Up to <code>'B'<\/code> is 01. Down to <code>'A'<\/code> is FF, which is -1 in two-digit hex. The C9 is the newline.<\/p>\n<p>The true usefulness of this encoding, and the test of your programming prowess, comes with writing the decoding filter. The decoding program is the topic of <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5117\">next month&#8217;s Exercise<\/a>. You can start creating now or wait until New Year&#8217;s Day, which is what I&#8217;m going to do.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The task for this month&#8217;s Exercise is to write an encoding filter that follows a specific pattern: After the first character (or byte) is output as a 2-digit hex value, the remaining characters are output as the difference between the &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5083\">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":[5],"tags":[],"class_list":["post-5083","post","type-post","status-publish","format-standard","hentry","category-solution"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5083","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=5083"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5083\/revisions"}],"predecessor-version":[{"id":5144,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5083\/revisions\/5144"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5083"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5083"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5083"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}