{"id":1601,"date":"2015-10-24T00:01:49","date_gmt":"2015-10-24T07:01:49","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1601"},"modified":"2021-09-23T10:48:23","modified_gmt":"2021-09-23T17:48:23","slug":"encoding-and-decoding-part-i","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1601","title":{"rendered":"Encoding and Decoding, Part I"},"content":{"rendered":"<p>A good way to exercise your C programming muscles is to work on a encoding\/decoding project. This process makes you think about data and how it&#8217;s represented, and also how to work on both ends of an input\/output puzzle.<br \/>\n<!--more--><br \/>\nEncoding is the process of translating information into another format. For example, <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3869\">Morse Code<\/a> translates letters and numbers into dots and dashes.<\/p>\n<p>At the other end, decoding translates the code created back into its original form, such as taking the dots and dashes and converting them back into readable text.<\/p>\n<p>Don&#8217;t confuse encoding with compression or encryption. While these could be attributes of encoding, they&#8217;re not required features. As with encoding, you can compress and decompress data, encrypt it or decrypt it, or a mixture of each. In all cases, the original data is recovered, which is the point of encoding and decoding.<\/p>\n<p>A simple example of encoding would be to input a string as text and output that string as its ASCII code values, using either decimal or hex values. Send those values into another program to decode and retrieve the original text.<\/p>\n<p>Another example, one I touch upon in my books, is the exclusive OR operation using byte 0xAA, binary 10101010. If you XOR 0xAA any byte and then XOR 0xAA the result, you get back the original value.<\/p>\n<p>The following code reads text from standard input and outputs a string of hexadecimal bytes encoded with XOR 0xAA.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int ch;\r\n\r\n    while(1)\r\n    {\r\n        ch = fgetc(stdin);\r\n        if(ch == EOF)\r\n            break;\r\n        printf(\"%02X\",ch^0xAA);\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>while<\/em> loop at Lines 7 through 13 is endless. Standard input is read at Line 9 and stored in variable <code>ch<\/code>. If the end of file is captured at Line 10, the loop halts. Otherwise, the value of <code>ch<\/code> is XOR&#8217;d with 0xAA in the <em>printf()<\/em> statement at Line 12, and output as a 2-digit hexadecimal byte.<\/p>\n<p>Yes, this program is a filter, which reads standard input and generates standard output. You can run it by itself, which is weird, or redirect input from another source.<\/p>\n<p>If you run the code by itself, type some text and press the Enter key to see it processed. Press Ctrl+C to end input or, to generate the EOF character, type Ctrl+Z in Windows or Ctrl+D in Unix. Here&#8217;s a sample run at the command prompt:<\/p>\n<pre><code>Hello there, Dan!\r\nE2CFC6C6C58ADEC2CFD8CF868AEECBC48BA0<\/code><\/pre>\n<p>Assuming the program is named <code>1024<\/code> and you have a file named <a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2015\/09\/gettysburg.txt\"><code>gettysburg.txt<\/code><\/a> in the current directory, you can use the filter with input redirection to encode the file&#8217;s text:<\/p>\n<pre><code>.\/1024 < gettysburg.txt \r\nECC5DFD88AD9C9C5D8CF8ACBC4CE8AD9CFDCCFC48AD3CFCBD8D98ACBCDC58AC5DFD88ACCCBDEC2CFD8D98AC8D8C5DFCDC2DE8ACCC5D8DEC28AC5C48ADEC2C3D98AC9C5C4DEC3C4CFC4DE8ACB8AC4CFDD8AC4CBDEC3C5C4868AC9C5C4C9CFC3DCCFCE8AC3C48AC6C3C8CFD8DED3868ACBC4CE8ACECFCEC3C9CBDECFCE8ADEC58ADEC2CF8ADAD8C5DAC5D9C3DEC3C5C48ADEC2CBDE8ACBC6C68AC7CFC48ACBD8CF8AC9D8CFCBDECFCE8ACFDBDFCBC684A0A0<\/code><\/pre>\n<p>The encoding works, but it's sloppy. First, the output isn't identifiable as encoded data, so a heading would help not only anyone reading the data but eventually assist the decoding program to recognize properly-encoded data.<\/p>\n<p>Second, the code could be formatted in a manner other than a long string of hex bytes. After all, this output is encoded. It's not an attempt at encryption or obfuscation, so making it pretty is a good thing.<\/p>\n<p>In <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1612\">next week's Lesson<\/a> I show some improvements to the code, which properly presents the output.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s not compression and it&#8217;s not encryption. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1601\">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-1601","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\/1601","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=1601"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1601\/revisions"}],"predecessor-version":[{"id":4993,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1601\/revisions\/4993"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1601"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1601"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1601"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}