{"id":2252,"date":"2016-12-24T00:01:59","date_gmt":"2016-12-24T08:01:59","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2252"},"modified":"2016-12-31T08:25:28","modified_gmt":"2016-12-31T16:25:28","slug":"from-text-to-hex","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2252","title":{"rendered":"From Hex to Text"},"content":{"rendered":"<p>Do you speak hex? As a programmer, do you look at <code>0xF<\/code> and see 1111 in binary? Do you see 15 decimal? What about the ASCII code? Do you know the letter for code <code>0x41<\/code>? Are you that good?<br \/>\n<!--more--><br \/>\nThe character for ASCII code <code>0x41<\/code> is <code>'A'<\/code>, which you&#8217;d know if you are either a total nerd or have been programming for some time &mdash; or both! I never set out to memorize hex \/ decimal \/ binary \/ ASCII; it just happened.<\/p>\n<p>Still, occasionally the nerds present information in a fashion designed to be easily picked up by nerds, but ignored by anyone else.<\/p>\n<p>Recently I saw a post about computer pioneer <a href=\"https:\/\/en.wikipedia.org\/wiki\/Dennis_Ritchie\" target=\"_blank\">Dennis Ritchie<\/a>. He died the same year as Steve Jobs, but few people lavish the praise upon Dr. Ritchie that they attribute to Jobs. In fact, Jobs contributions to computer science pale in comparison to what Dr. Ritchie did, including inventing the C language and Unix.<\/p>\n<p>I&#8217;ll leave it up to you to review Dr. Ritchie&#8217;s many accomplishments. Yet, recently I saw someone compare Ritchie and Jobs, at the end of which they wrote:<\/p>\n<p><code>79 6f 75 20 74 77 61 74 73<\/code><\/p>\n<p>The numbers I recognized as hexadecimal, thanks to the F in <code>6f<\/code>. I&#8217;ve not memorized the ASCII table completely, but I know that the codes represent characters &mdash; text. The <code>0x020<\/code> specifically is the space. To translate the rest of the codes into characters, I&#8217;d either have to examine an ASCII table or write code that translates the ASCII into text.<\/p>\n<p>I did the latter.<\/p>\n<p>Here you find the code I wrote to translate hex bytes, similar to those formatted above, and generate the corresponding ASCII string. The code doesn&#8217;t check input, so it&#8217;s not bulletproof. It&#8217;s something I wrote quickly just in case the issue came up again:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\n#define SIZE 32\r\n\r\nint main()\r\n{\r\n    char output[SIZE];\r\n    int c;\r\n    int x = 0;\r\n\r\n    printf(\"Type hex (l\/c) digits, 0 to end: \");\r\n    while(x &lt; SIZE)\r\n    {\r\n        scanf(\"%2x\",&c);\r\n        output[x] = (char)c;\r\n        if(c == 0) break;\r\n        x++;\r\n    }\r\n    printf(\"\\n%s\\n\",output);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Line 7 creates a buffer for 32 characters, which is 31 characters plus the <code>'\\0'<\/code> at the end of a string.<\/p>\n<p>The <em>while<\/em> loop at Line 12 uses variable <code>x<\/code> as an index to count characters. Text is read at Line 14 with the <em>scanf()<\/em> function, which is set to recognize 2-digit hexadecimal values as input.<\/p>\n<p>The character is stored in the <code>output[]<\/code> buffer at offset <code>x<\/code> in Line 15.<\/p>\n<p>Line 16 tests for value 0 as input, which terminates the loop before <code>x<\/code> reaches 32.<\/p>\n<p>Line 19 prints the result.<\/p>\n<p>Here is a sample run:<\/p>\n<pre><code>Type hex (l\/c) digits, 0 to end: 48 65 6c 6c 6f 21 0\r\n\r\nHello!<\/code><\/pre>\n<p>The <em>scanf()<\/em> function terminates input at any whitespace character, so I could type the hex digits each on a line by itself or just use spaces to separate each.<\/p>\n<p>Because I found this utility useful, I saved in my personal <code>bin<\/code> folder. I named it <em>hex2ascii<\/em>. Naturally, being a nerd, I felt the need to write a companion program <em>ascii2hex<\/em>, which works the other way: It translates a short text string into a series of hex bytes. I&#8217;ll show you that code in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2260\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Translate a series of hexadecimal ASCII values into a string of text. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2252\">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-2252","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\/2252","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=2252"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2252\/revisions"}],"predecessor-version":[{"id":2291,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2252\/revisions\/2291"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2252"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}