{"id":2260,"date":"2016-12-31T00:01:27","date_gmt":"2016-12-31T08:01:27","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2260"},"modified":"2016-12-24T09:04:59","modified_gmt":"2016-12-24T17:04:59","slug":"from-text-to-hex-2","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2260","title":{"rendered":"From Text to Hex"},"content":{"rendered":"<p>In <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2252\">last week&#8217;s Lesson<\/a>, I showed a program that translates a string of hex values into ASCII text. This code was to sate the nerd in me so that when another nerd writes <code>48 65 6c 6c 6f 2c 20 66 65 6c 6c 6f 77 20 6e 65 72 64 21<\/code>, I can respond accordingly. But to respond in hex, a second program is required, one that translates ASCII text into a string of hex values.<br \/>\n<!--more--><br \/>\nAs with the hex-to-ascii example, writing code that gobbles a string of text and spits out hexadecimal values isn&#8217;t really that complex: You ask for input, print each character in the string as a hex value, and you&#8217;re done. Here is such code:<\/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 string[SIZE];\r\n    int i = 0;\r\n\r\n    printf(\"Text to translate: \");\r\n    fgets(string,SIZE,stdin);\r\n    while(string[i] != '\\n')\r\n    {\r\n        printf(\"%2x \",string[i]);\r\n        i++;\r\n    }\r\n    putchar(string[i]);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Lines 10 and 11 prompt for input. I chose to use the <em>fgets()<\/em> function in Line 11, which helps check the size of the input buffer, <code>string[]<\/code>.<\/p>\n<p>Line 12 starts a <em>while<\/em> loop to process the input buffer. The <code>%2x<\/code> format for output, which displays each character value as a 2-digit, lower case, hex number. The loop continues until the newline (<code>'\\n'<\/code>) is found, which is when <em>fgets()<\/em> stops reading input.<\/p>\n<p>Because <code>i<\/code> still references the newline in the <code>string[]<\/code> array, Line 17 prints that character, which makes for clean output.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<pre><code>Text to translate: Oh my goodness!\r\n4f 68 20 6d 79 20 67 6f 6f 64 6e 65 73 73 21<\/code><\/pre>\n<p>You can take those values and plug them into the program from last week&#8217;s Lesson to translate back the text:<\/p>\n<pre><code>Type hex (l\/c) digits, 0 to end: 4f 68 20 6d 79 20 67 6f 6f 64 6e 65 73 73 21 0\r\n\r\nOh my goodness!<\/code><\/pre>\n<p>And the nerd in me is quite content, thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Translate a string of text into a series of hexadecimal ASCII values. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2260\">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-2260","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\/2260","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=2260"}],"version-history":[{"count":2,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2260\/revisions"}],"predecessor-version":[{"id":2269,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2260\/revisions\/2269"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2260"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2260"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2260"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}