{"id":1633,"date":"2015-11-08T00:01:57","date_gmt":"2015-11-08T08:01:57","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1633"},"modified":"2015-11-07T08:53:57","modified_gmt":"2015-11-07T16:53:57","slug":"hex-parsing-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1633","title":{"rendered":"Hex Parsing &#8211; Solution"},"content":{"rendered":"<p>Two solutions are possible for <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1610\">this month&#8217;s Exercise<\/a>, a string solution and a value solution. One is good for parsing the hex string right-to-left, the other for parsing the string left-to-right.<br \/>\n<!--more--><br \/>\nCommon to both solutions is the random number generation; where the solutions differ is how the number is stored.<\/p>\n<p>For the string solution, the number is stored as a string in a buffer. The <em>sprintf()<\/em> function handles that task:<\/p>\n<pre class=\"screen\">\r\n    <span class=\"comments\">\/* Generate random number *\/<\/span>\r\n    srand( (unsigned)time(NULL) );\r\n    r = rand();\r\n\r\n    <span class=\"comments\">\/* convert value to string *\/<\/span>\r\n    sprintf(buffer,\"%X\",r);\r\n    printf(\"Original value: %s\\n\",buffer);<\/pre>\n<p>Once the value is stored as a string (in <code>buffer<\/code>, above), the code needs to locate the final character in the hexadecimal number. That&#8217;s necessary because the random value could be any length up to 8 characters (depending on the compiler). Once found, the string is displayed backwards (right-to-left) one character at a time. Here&#8217;s the code I used to perform that task, where <code>buffer<\/code> is the string&#8217;s location and <code>b<\/code> is a pointer variable:<\/p>\n<pre class=\"screen\">\r\n    <span class=\"comments\">\/* find last character *\/<\/span>\r\n    b = buffer;\r\n    while(*b++);    <span class=\"comments\">\/* set b-&gt;'\\0' +1 *\/<\/span>\r\n    b-=2;           <span class=\"comments\">\/* back up to last char *\/<\/span>\r\n    while(b &gt;= buffer)\r\n        printf(\"%c\\n\",*b--);<\/pre>\n<p><a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2015\/10\/11exercise-a.c\">Click here<\/a> to view the entire code.<\/p>\n<p>The other solution, the one you probably didn&#8217;t try, is to work with the random value as a value. The secret to making the solution work is to shift the value&#8217;s bits to the right and chop off one hexadecimal value (byte) at a time. Here is that crazy solution:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;time.h&gt;\r\n\r\nint main()\r\n{\r\n    int r,digits,byte;\r\n\r\n    <span class=\"comments\">\/* Generate random number *\/<\/span>\r\n    srand( (unsigned)time(NULL) );\r\n    r = rand();\r\n\r\n    printf(\"Original value: %X\\n\",r);\r\n\r\n    digits = 8;\r\n    while(digits)\r\n    {\r\n        byte = r & 0xF;\r\n        printf(\"%X\\n\",byte);\r\n        r = r &gt;&gt; 4;\r\n        if( r == 0 )\r\n            break;\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>This solution is really nerdy. As an ancient Assembly language programmer, this code demonstrates my primitive level of thinking regarding binary data.<\/p>\n<p>Variable <code>digits<\/code> (Line 15) is set to 8, which is the maximum number of digits in an <em>int<\/em> value for most compilers. The <em>while<\/em> loop spins through those digits, starting at Line 16.<\/p>\n<p>At Line 18, an AND mask peels off all but the last byte of the random value. The <em>printf()<\/em> statement in Line 19 uses <code>%X<\/code> placeholder to display that byte. Because it&#8217;s the last digit in the value, this statement satisfies the Exercise&#8217;s solution.<\/p>\n<p>To display the next byte, the entire value is shifted to the right by 4 bits in Line 20. If the value is equal to zero (Line 21), then the last digit was displayed and the loop can terminate. Otherwise, the loop repeats, displaying the far right hex value.<\/p>\n<p>If you want to visually see how the process works, change the <em>printf()<\/em> statement at Line 19 to read:<\/p>\n<pre class=\"screen\">\r\n        printf(\"%8X - %X\\n\",r,byte);<\/pre>\n<p>Here&#8217;s sample output:<\/p>\n<pre><code>Original value: 330CAC77\r\n330CAC77 - 7\r\n 330CAC7 - 7\r\n  330CAC - C\r\n   330CA - A\r\n    330C - C\r\n     330 - 0\r\n      33 - 3\r\n       3 - 3<\/code><\/pre>\n<p>Above, you see the value shifted right 4 bits. Each time the AND mask helps lop off the final value.<\/p>\n<p>You may have arrived at your own, clever solution. If so, great! As long as the output shows each digit from the hex value, right-to-left, you completed the hex parsing Exercise.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Two solutions are possible for this month&#8217;s Exercise, a string solution and a value solution. One is good for parsing the hex string right-to-left, the other for parsing the string left-to-right.<\/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-1633","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\/1633","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=1633"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1633\/revisions"}],"predecessor-version":[{"id":1648,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1633\/revisions\/1648"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1633"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1633"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1633"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}