{"id":1612,"date":"2015-10-31T00:01:07","date_gmt":"2015-10-31T07:01:07","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1612"},"modified":"2015-11-07T08:06:44","modified_gmt":"2015-11-07T16:06:44","slug":"encoding-and-decoding-part-ii","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1612","title":{"rendered":"Encoding and Decoding, Part II"},"content":{"rendered":"<p>To help improve the presentation of encoded data, consider sprucing up the output.<br \/>\n<!--more--><br \/>\nIn <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1601\">last week&#8217;s Lesson<\/a>, the XOR 0xAA encoding program read text input and generated hexadecimal output. Here&#8217;s the typical result of running the program on the <a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2015\/09\/gettysburg.txt\"><code>gettysburg.txt<\/code><\/a> file:<\/p>\n<pre><code>.\/1024 < gettysburg.txt \r\nECC5DFD88AD9C9C5D8CF8ACBC4CE8AD9CFDCCFC48AD3CFCBD8D98ACBCDC58AC5DFD88ACCCBDEC2CFD8D98AC8D8C5DFCDC2DE8ACCC5D8DEC28AC5C48ADEC2C3D98AC9C5C4DEC3C4CFC4DE8ACB8AC4CFDD8AC4CBDEC3C5C4868AC9C5C4C9CFC3DCCFCE8AC3C48AC6C3C8CFD8DED3868ACBC4CE8ACECFCEC3C9CBDECFCE8ADEC58ADEC2CF8ADAD8C5DAC5D9C3DEC3C5C48ADEC2CBDE8ACBC6C68AC7CFC48ACBD8CF8AC9D8CFCBDECFCE8ACFDBDFCBC684A0A0<\/code><\/pre>\n<p>As formatted on this web page, the encoded text is just a long string, which you have to scroll left and right to see. On the terminal screen, it looks just as ugly.<\/p>\n<p>Because a specific method is used to encode the text, and the long-term plan is to decode the text, a better form of output is a positive thing. To be helpful to the decoder program, the output could be formatted. It can include a header that describes the encoding format. The data itself can be organized so that the decoding program has an easier time fetching the data &mdash; and humans can more easily view the data. And finally, an ending tag can wrap up the encoded data, confirming that it's been properly encoded and read.<\/p>\n<p>To meet these ends, I've crafted the following improvement to the original encoding program, which I call <em>hexcode<\/em>:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\n#define BYTES_PER_LINE 24\r\n\r\nint main()\r\n{\r\n    int ch,bytes;\r\n\r\n    bytes = 0;\r\n    printf(\"START HEX CODE v1.0\\n\");\r\n    while(1)\r\n    {\r\n        ch = fgetc(stdin);\r\n        bytes++;\r\n        if(ch == EOF)\r\n            break;\r\n        printf(\" %02X\",ch^0xAA);\r\n        if( bytes == BYTES_PER_LINE)\r\n        {\r\n            putchar('\\n');\r\n            bytes = 0;\r\n        }\r\n    }\r\n    printf(\"\\nEND HEX CODE\\n\");\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The defined constant <code>BYTES_PER_LINE<\/code> at Line 3 sets the number of hex byte chunks displayed in each line (row) of output. This value is used with the <code>bytes<\/code> variable, which is initialized at Line 9, then incremented at Line 14 for each character read. When the value of <code>bytes<\/code> is equal to the <code>BYTES_PER_LINE<\/code> constant (Line 18), the <code>bytes<\/code> variable is reset, and a newline is output to start over with the next row of formatted text.<\/p>\n<p>The first line output is the program's name and version number, at Line 10. I added the word <code>START<\/code> mostly for human eyes, but the program name can be used by the decoder to confirm the data format. The version number also helps the decoder to recognize how the data is encoded, should the format change in the future.<\/p>\n<p>At Line 17, the <em>printf()<\/em> statement's output is modified to add a space between each hexadecimal value. The XOR 0xAA function still takes place at that line.<\/p>\n<p>Finally, I added a end tag, output with <em>printf()<\/em> at Line 24.<\/p>\n<p>Here is the sample output for the <code>gettysburg.txt<\/code> file, which was shown earlier in this post using the original hexcode program:<\/p>\n<pre><code>$ .\/hexcode < gettysburg.txt \r\nSTART HEX CODE v1.0\r\n EC C5 DF D8 8A D9 C9 C5 D8 CF 8A CB C4 CE 8A D9 CF DC CF C4 8A D3 CF CB\r\n D8 D9 8A CB CD C5 8A C5 DF D8 8A CC CB DE C2 CF D8 D9 8A C8 D8 C5 DF CD\r\n C2 DE 8A CC C5 D8 DE C2 8A C5 C4 8A DE C2 C3 D9 8A C9 C5 C4 DE C3 C4 CF\r\n C4 DE 8A CB 8A C4 CF DD 8A C4 CB DE C3 C5 C4 86 8A C9 C5 C4 C9 CF C3 DC\r\n CF CE 8A C3 C4 8A C6 C3 C8 CF D8 DE D3 86 8A CB C4 CE 8A CE CF CE C3 C9\r\n CB DE CF CE 8A DE C5 8A DE C2 CF 8A DA D8 C5 DA C5 D9 C3 DE C3 C5 C4 8A\r\n DE C2 CB DE 8A CB C6 C6 8A C7 CF C4 8A CB D8 CF 8A C9 D8 CF CB DE CF CE\r\n 8A CF DB DF CB C6 84 A0 A0\r\nEND HEX CODE<\/code><\/pre>\n<p>Much better!<\/p>\n<p>In <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1618\">next week's Lesson<\/a>, the decoding process begins. The first step is to convert a hexadecimal character string into a value.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Make the output pretty &mdash; and predictable. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1612\">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-1612","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\/1612","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=1612"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1612\/revisions"}],"predecessor-version":[{"id":1641,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1612\/revisions\/1641"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1612"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1612"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1612"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}