{"id":7266,"date":"2025-12-06T00:01:59","date_gmt":"2025-12-06T08:01:59","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7266"},"modified":"2025-12-13T08:19:37","modified_gmt":"2025-12-13T16:19:37","slug":"dumping-the-screen-in-color","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7266","title":{"rendered":"Dumping the Screen in Color"},"content":{"rendered":"<p>The <em>hexdump<\/em> utility is a marvelous tool for grabbing a sneak peek at a file&#8217;s innards, especially when debugging code that performs file access. As a text mode tool, however, it could stand to use some colorful character improvement.<br \/>\n<!--more--><br \/>\nThe <em>hexdump<\/em> utility is a filter. Run at the command prompt, it dumps a file by name or by using redirected input. Here&#8217;s a quickie test run:<\/p>\n<p><code>~$ hexdump<br \/>\nHello!<br \/>\n0000000 6548 6c6c 216f 000a<br \/>\n0000007<br \/>\n~$<\/code><\/p>\n<p>My favorite view is &#8220;canonical&#8221; mode, activated by using the <code>-C<\/code> switch:<\/p>\n<p><code>~$ hexdump -C<br \/>\nHello!<br \/>\n00000000&nbsp;&nbsp;48&nbsp;65&nbsp;6c&nbsp;6c&nbsp;6f&nbsp;21&nbsp;0a&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|Hello!.|<br \/>\n00000007<br \/>\n~$<\/code><\/p>\n<p>Seeing both the hex dump and ASCII equivalents can truly help disclose a file&#8217;s contents. In fact, I used <em>hextdump<\/em> a few days ago to dump some word processing files saved years ago by antique software. This tool helped me view the text portion of correspondence, which saves time tussling with Microsoft Word to extract text.<\/p>\n<p>The <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4005\">Exercise for March 2020<\/a> was to code a <em>hexdump<\/em>-like utility. My solution involved reading a file for input, though it can easily be converted into a filter for more flexible input. I&#8217;ll use this program as a base for my updated <em>hexdump<\/em>\/color utility, though it requires lotsa modification.<\/p>\n<p>My first goal is to decide how to use colors to help present more than just a period in the ASCII column for non-printable characters. For example, control codes can be output color-coded red. Each code has a corresponding character: ^@ for the null character (\\0), ^A for control code 1, ^B for 2, on up to ^_ for code 31. No standard equivalent exists for code 127, the &#8220;delete&#8221; character: Unicode U-2421 (&#9249;) is often used on the web as well as U-247F ( &#9343; for some reason).<\/p>\n<p>The following code churns through ASCII values zero through 127. For the control codes (values zero through 31), the corresponding character is output in red:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_12_06-Lesson.c\" rel=\"noopener\" target=\"_blank\">2025_12_06-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\n#define RED \"\\e[31m\"\r\n#define NORMAL \"\\e[m\"\r\n\r\nint main()\r\n{\r\n    unsigned char ch;\r\n\r\n    for( ch=0; ch&lt;=127; ch++ )\r\n    {\r\n        printf(\"%02X %03d \",ch,ch);\r\n        if( ch&lt;32 )\r\n            printf(\"%s%c%s\",RED,ch+'@',NORMAL);\r\n        else\r\n            putchar(ch);\r\n        putchar('\\n');\r\n    }\r\n    return 0;\r\n}<\/pre>\n<p>Variable <code>ch<\/code> is declared as an <em>unsigned char<\/em>, which prevents the <em>for<\/em> loop from repeating endlessly. (A signed value resets <em>ch<\/em> negative after 127 and the loop continues endlessly.)<\/p>\n<p>Within the <em>for<\/em> loop, the hexadecimal and decimal values of variable <code>ch<\/code> are output. When <code>ch<\/code> is less than 32, its ASCII equivalent character is output color-coded red. Otherwise, the character is output directly. Refer to <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5270\">this blog post<\/a> for information on ANSI color text output.<\/p>\n<p>Here&#8217;s a truncated sample run:<\/p>\n<p><code>00 000 <span style=\"color:red;\">@<\/span><br \/>\n01 001 <span style=\"color:red;\">A<\/span><br \/>\n02 002 <span style=\"color:red;\">B<\/span><br \/>\n03 003 <span style=\"color:red;\">C<\/span><br \/>\n04 004 <span style=\"color:red;\">D<\/span><br \/>\n05 005 <span style=\"color:red;\">E<\/span><br \/>\n06 006 <span style=\"color:red;\">F<\/span><br \/>\n07 007 <span style=\"color:red;\">G<\/span><br \/>\n...<br \/>\n78 120 x<br \/>\n79 121 y<br \/>\n7A 122 z<br \/>\n7B 123 {<br \/>\n7C 124 |<br \/>\n7D 125 }<br \/>\n7E 126 ~<br \/>\n7F 127<\/code><\/p>\n<p>For value 127 (del), I&#8217;d like to output the Unicode character &#9249; as the equivalent. Additionally, for character code values 128 through 255, special characters can also be generated, perhaps in color. In fact, equivalents for the control code characters exist as well: code 0, ^@ is &#9216;. This update to the code requires that I retrofit it with wide character output. I begin this task in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7287\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of my favorite command line tools can do with some colorful improvement. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7266\">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-7266","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\/7266","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=7266"}],"version-history":[{"count":13,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7266\/revisions"}],"predecessor-version":[{"id":7310,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7266\/revisions\/7310"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7266"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7266"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7266"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}