{"id":4005,"date":"2020-03-01T00:01:29","date_gmt":"2020-03-01T08:01:29","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4005"},"modified":"2020-03-08T08:25:51","modified_gmt":"2020-03-08T15:25:51","slug":"dump-that-file","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4005","title":{"rendered":"Dump That File!"},"content":{"rendered":"<p>One of the many, useful tools a programmer must have is a hexdump utility. The utility consumes a file&#8217;s raw bytes and outputs them in a human-readable manner. By examining the dump, you can determine if file contains the proper data in the correct format, as well as do other interesting, useful, and technerd things.<br \/>\n<!--more--><br \/>\nThe traditional Unix utility is <em>hexdump<\/em>, which outputs file in a format like this (by using the -C switch):<\/p>\n<pre>00000000  4f 6e 65 20 6f 66 20 74  68 65 20 74 72 61 64 69  |One of the tradi|\r\n00000010  74 69 6f 6e 61 6c 20 55  6e 69 78 20 75 74 69 6c  |tional Unix util|\r\n00000020  69 74 69 65 73 20 69 73  20 3c 65 6d 3e 68 65 78  |ities is &lt;em&gt;hex|\r\n00000030  64 75 6d 70 3c 2f 65 6d  3e 2c 20 77 68 69 63 68  |dump&lt;\/em&gt;, which|\r\n00000040  20 6f 75 74 70 75 74 73  20 66 69 6c 65 20 69 6e  | outputs file in|\r\n00000050  20 61 20 66 6f 72 6d 61  74 20 6c 69 6b 65 20 74  | a format like t|\r\n00000060  68 69 73 3a 0a                                    |his:.|\r\n00000065<\/pre>\n<p>The last line lists the number of bytes in the file, 65 hex or 101 characters. The file examined was a text file, though any file can be output as raw data; for non-printable characters, a dot (period) appears in the text column. Further, the <em>hexdump<\/em> utility skips over duplicate rows of zero bytes, which keeps the output short.<\/p>\n<p>Your challenge for this month&#8217;s Exercise is to write a hexdump-like utility. Have it output the contents of any file, specified on the command line, in a multi-column format with 2-digit hex bytes and an ASCII\/character column. At the end of output, display a total byte count.<\/p>\n<p>Here is a sample run of my solution:<\/p>\n<pre>Dump of 'a.out':\r\n0000  CF FA ED FE 07 00 00 01 - 03 00 00 80 02 00 00 00   ................\r\n0010  10 00 00 00 58 05 00 00 - 85 00 20 00 00 00 00 00   ....X..... .....\r\n0020  19 00 00 00 48 00 00 00 - 5F 5F 50 41 47 45 5A 45   ....H...__PAGEZE\r\n0030  52 4F 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   RO..............\r\n0040  00 00 00 00 01 00 00 00 - 00 00 00 00 00 00 00 00   ................\r\n0050  61 74 65 00                                         ate.\r\n 84 total bytes<\/pre>\n<p>See how the final line, <code>0050<\/code>, is terminated? Only four hex bytes are output, then a lot of whitespace, and finally the character equivalents. Also, the total byte value is given in decimal, not hex. Duplicate this presentation in your program as well, which is similar to how the <em>hexdump<\/em> utility works.<\/p>\n<p>Please try this Exercise on your own before you peek at <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4022\">my solution<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Write code that outputs a file&#8217;s data in hexadecimal and text characters. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4005\">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":[3],"tags":[17],"class_list":["post-4005","post","type-post","status-publish","format-standard","hentry","category-exercise","tag-hexdump"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4005","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=4005"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4005\/revisions"}],"predecessor-version":[{"id":4040,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4005\/revisions\/4040"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4005"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4005"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4005"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}