{"id":1618,"date":"2015-11-07T00:01:58","date_gmt":"2015-11-07T08:01:58","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1618"},"modified":"2015-11-14T07:47:31","modified_gmt":"2015-11-14T15:47:31","slug":"encoding-and-decoding-part-iii","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1618","title":{"rendered":"Encoding and Decoding, Part III"},"content":{"rendered":"<p>You can output values in the hexadecimal format, thanks to the <code>%x<\/code> and <code>%X<\/code> placeholders in the <em>printf()<\/em> function. Getting hex strings as input is also possible, but not entirely obvious.<br \/>\n<!--more--><br \/>\nTo continue from <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1612\">last week&#8217;s Lesson<\/a>, the encoded file outputs bytes as hexadecimal values, which appear as strings. To decode that encoded information, the hexadecimal strings must be converted to their decimal values.<\/p>\n<p>The good news is that the <em>scanf()<\/em> function borrows the <code>%x<\/code> and <code>%X<\/code> placeholders from <em>printf()<\/em>. So it&#8217;s possible to read a hexadecimal string and convert it into a value, as the following code demonstrates:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int d;\r\n\r\n    printf(\"Type a hex value: \");\r\n    scanf(\"%2x\",&d);\r\n    printf(\"0x%2x is %d decimal.\\n\",d,d);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>scanf()<\/em> function at Line 8 reads the first two digits of input and converts them to a hexadecimal value, stored in variable <code>d<\/code>. The value is output in Line 9.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<pre><code>Type a hex value: AA\r\n0xaa is 170 decimal.<\/code><\/pre>\n<p>The difference between <code>%x<\/code> and <code>%X<\/code> is whether the output is lowercase or uppercase, but for input it apparently doesn&#8217;t matter (at least not on my compiler).<\/p>\n<p>You can also decode hexadecimal manually. Yes, you&#8217;re reinventing the wheel, but this solution is more flexible than using <em>scanf()<\/em>.<\/p>\n<p>In the following code, input is saved as a two-byte string, <code>hex<\/code>. Each character is then extrapolated as a hexadecimal digit, the first digit is adjusted, and the final value displayed.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;ctype.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\nint hex2int(char *hex);\r\nint char2int(char h);\r\n\r\nint main()\r\n{\r\n    int v;\r\n    char hex[3];\r\n\r\n    hex[0] = (char)fgetc(stdin);\r\n    hex[1] = (char)fgetc(stdin);\r\n    hex[2] = '\\0';  <span class=\"comments\">\/* create string *\/<\/span>\r\n    v = hex2int(hex);\r\n    printf(\"0x%s = %d\\n\",hex,v);\r\n\r\n    return(0);\r\n}\r\n\r\nint hex2int(char *hex)\r\n{\r\n    int a,b;\r\n\r\n    a = char2int(*(hex+0)); <span class=\"comments\">\/* first character *\/<\/span>\r\n    b = char2int(*(hex+1)); <span class=\"comments\">\/* second char *\/<\/span>\r\n\r\n    return( (a &lt;&lt; 4) + b);\r\n}\r\n\r\nint char2int(char h)\r\n{\r\n    if( isnumber(h) )\r\n        return( h - '0');\r\n    h = toupper(h);\r\n    if( h >= 'A' && h <= 'F' )\r\n        return( h - 'A' + 0x0A);\r\n    <span class=\"comments\">\/* invalid character *\/<\/span>\r\n    {   \r\n        printf(\"\\nInvalid hex character: %c\\n\",h);\r\n        exit(1);\r\n    }   \r\n\r\n    return(0);\r\n}<\/pre>\n<p>Two bytes are read and stored into the string <code>hex<\/code> at Lines 13 and 14. The <em>hex2int()<\/em> function converts the value at Line 15.<\/p>\n<p>At Line 22, the <em>hex2int()<\/em> function accepts a two-character string as an argument. Each character in the string is converted to a value, at Lines 26 and 27. The values are manipulated so that value <code>a<\/code> (the first digit in the number) is multiplied by 16; it&#8217;s shifted left four digits. The second value is added to the first, which creates and returns the hexadecimal byte value.<\/p>\n<p>At Line 32, individual hex characters are converted to values. The <em>isnumber()<\/em> function at Line 33 works to return characters 0 through 9 as a value. A similar calculation is made on letters in Line 38. When the letter falls outside the range A to F, the <em>else<\/em> condition displays an error message and the program terminates.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<pre><code>AA\r\n0xAA = 170<\/code><\/pre>\n<p>Above, I typed <strong>AA<\/strong> at the prompt and then pressed the Enter key. The program evaluated 0xAA and returned the proper decimal representation, 170.<\/p>\n<p>Now that the hex conversion issue is solved, then next step is to begin writing the decoding program. That process begins in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1628\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Translating hexadecimal strings into values. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1618\">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-1618","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\/1618","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=1618"}],"version-history":[{"count":8,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1618\/revisions"}],"predecessor-version":[{"id":1652,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1618\/revisions\/1652"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1618"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1618"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1618"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}