{"id":5132,"date":"2022-01-08T00:01:55","date_gmt":"2022-01-08T08:01:55","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5132"},"modified":"2022-01-01T10:19:11","modified_gmt":"2022-01-01T18:19:11","slug":"decoding-a-string-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5132","title":{"rendered":"Decoding a String &#8211; Solution"},"content":{"rendered":"<p>Encoding means nothing if you can&#8217;t decode, which is the task for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5117\">this month&#8217;s Exercise<\/a>: Transform the encoded hex bytes back into characters, unwinding the formula used to create them. The challenge isn&#8217;t really as much with coding the math as it is with translating hex bytes back into integer values &mdash; characters.<br \/>\n<!--more--><br \/>\nBeing a filter, this Exercise&#8217;s solution runs as a loop. It consumes bytes until the <code>EOF<\/code> is encountered. For my version of the solution, I opted not to store the characters, but rather convert them one by one. The first character input is stored in <em>int<\/em> variable <code>a<\/code>, the second in <em>int<\/em> variable <code>b<\/code>:<\/p>\n<pre class=\"screen\">\r\n<span class=\"comments\">\/* obtain first hex digit *\/<\/span>\r\nch = getchar();\r\nif( ch==EOF ) break;\r\na = hexdigit(ch);\r\nif( a&lt;0 ) break;\r\n\r\n<span class=\"comments\">\/* obtain second hex digit *\/<\/span>\r\nch = getchar();\r\nif( ch==EOF ) break;\r\nb = hexdigit(ch);\r\nif( b&lt;0 ) break;<\/pre>\n<p>Both blocks of code use the same sequence: read the character, check for the <code>EOF<\/code> and stop if so. Use the <em>hexdigit()<\/em> function to convert the character into a value. Check the value&#8217;s range, terminating the filter on an invalid hex digit.<\/p>\n<p>Here is the <em>hexdigit()<\/em> function:<\/p>\n<pre class=\"screen\">\r\n<span class=\"comments\">\/* return a hex digit integer value or\r\n   -1 for an invalid character *\/<\/span>\r\nint hexdigit(char c)\r\n{\r\n    if( c>='A' && c<='F' )\r\n        return( c - 'A' + 10);\r\n    else if( c&gt;='0' &amp;&amp; c&lt;='9' )\r\n        return(c - '0');\r\n    else\r\n        return(-1);\r\n}<\/pre>\n<p>The <em>if<\/em> comparison checks for uppercase hexadecimal letters, A through F. Lowercase letters aren't checked in that the encoding program generates only uppercase. When a letter is encountered, it's integer value is returned, 10 through 15.<\/p>\n<p>The <em>else if<\/em> condition captures digits 0 through 9. These values are returned as their integer values, 0 through 9.<\/p>\n<p>When both of these comparisons fail, the function returns -1, an error. This return is what's examined in the <em>main()<\/em> function, which terminates the filter program on the error.<\/p>\n<p>To build the character, the <em>main()<\/em> function uses values stored in variables <code>a<\/code> and <code>b<\/code>:<\/p>\n<pre class=\"screen\">\r\n<span class=\"comments\">\/* build a byte using the current hex\r\n   digit plus the previous digit in\r\n   byte *\/<\/span>\r\nbyte += (a&lt;&lt;4) + b;\r\nputchar(byte);<\/pre>\n<p>The value of <code>a<\/code> shifted left four places and <code>b<\/code> retained. The result is the encoded value, which is added to the previous character output, stored in variable <code>byte<\/code>. Variable <code>byte<\/code> is initialized to zero, which is how the first character's ASCII value is output: as-is.<\/p>\n<p><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_01-Exercise.c\" rel=\"noopener\" target=\"_blank\">Click here<\/a> to view my full solution at Github.<\/p>\n<p>I hope your solution met with success. My first solution tried to read both hex digits at the same time, converting them to a single byte value. Only when I decided to read the characters one at a time did the solution finally occur to me. It made the <em>hexdigit()<\/em> function easier to code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Encoding means nothing if you can&#8217;t decode, which is the task for this month&#8217;s Exercise: Transform the encoded hex bytes back into characters, unwinding the formula used to create them. The challenge isn&#8217;t really as much with coding the math &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5132\">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-5132","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\/5132","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=5132"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5132\/revisions"}],"predecessor-version":[{"id":5151,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5132\/revisions\/5151"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5132"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5132"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}