{"id":112,"date":"2013-06-15T00:01:44","date_gmt":"2013-06-15T08:01:44","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=112"},"modified":"2013-06-08T06:37:30","modified_gmt":"2013-06-08T14:37:30","slug":"say-it-in-binary","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=112","title":{"rendered":"Say It in Binary"},"content":{"rendered":"<p><code>01010011011000010111100100100000010010010111010000100000<br \/>\n01101001011011100010000001000010011010010110111001100001<br \/>\n011100100111100100001010<\/code><br \/>\n<!--more--><br \/>\nThe bits above represent the ASCII code values for the text, &#8220;Say It in Binary.&#8221; That&#8217;s because I ran that text through this week&#8217;s program, which is designed to take up to 63 characters of text and spew out the binary values of the text&#8217;s characters (the ASCII codes).<\/p>\n<p><em>Why would anyone do this?<\/em><\/p>\n<p>I needed to place some binary text into an illustration for a project I&#8217;m working on. Rather than type <code>1010101<\/code> over and over again, I decided to code a quick program in C. That program would take text and convert it into a long, binary string. That way I could place an Easter Egg of sorts into the illustration.<\/p>\n<p>Easter Eggs are nothing new in my books. The original <em>C For Dummies<\/em> featured one, which is discussed on <a href=\"http:\/\/www.eeggs.com\/items\/54525.html\" target=\"_blank\">this web page<\/a>. Now I&#8217;ve created another one, by using the sample code shown below.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nchar *binbin(int n);\r\n\r\nint main()\r\n{\r\n    char input[64];\r\n    int x = 0;\r\n\r\n    printf(\"Type the text: \");\r\n    fgets(input,63,stdin);\r\n\r\n    while(input[x] != '\\0')\r\n        printf(\"%s\",binbin(input[x++]));\r\n    putchar('\\n');\r\n\r\n    return(0);\r\n}\r\n\r\nchar *binbin(int n)\r\n{\r\n    static char bin[9];\r\n    int x;\r\n\r\n    for(x=0;x&lt;8;x++)\r\n    {\r\n        bin[x] = n & 0x80 ? '1' : '0';\r\n        n &lt;&lt;= 1;\r\n    }\r\n    bin[x] = '\\0';\r\n    return(bin);\r\n}<\/pre>\n<p>This code uses my <em>binbin()<\/em> function, which translates an <code>int<\/code> value into an 8-bit string of binary digits. The <em>main()<\/em> function reads a string at Line 11. A <code>while<\/code> loop then processes the string one character at a time, using the <em>binbin()<\/em> function on that individual character (Lines 13 and 14).<\/p>\n<p>Because ASCII uses only 7 bits, the code could be modified to display only those 7 bits. If you note in the output (above), each initial bit (the 8th bit) is always zero. That modification involves a change to the <em>binbin()<\/em> function, which you can try on your own as a fun little exercise.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The code from this lesson takes a string of text and converts it into a long string of binary digits. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=112\">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-112","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\/112","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=112"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/112\/revisions"}],"predecessor-version":[{"id":122,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/112\/revisions\/122"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=112"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=112"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=112"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}