{"id":3032,"date":"2018-04-07T00:01:56","date_gmt":"2018-04-07T07:01:56","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=3032"},"modified":"2018-04-14T07:57:02","modified_gmt":"2018-04-14T14:57:02","slug":"from-binary-to-string","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3032","title":{"rendered":"From Binary to String"},"content":{"rendered":"<p>In my books, I use the <em>binbin()<\/em> function to display binary values. It&#8217;s a good way to demonstrate some of the C language&#8217;s low-level bitwise functions: The <em>binbin()<\/em> function lets you see the effect of bitwise operations at a binary level, all those ones and zeros.<br \/>\n<!--more--><br \/>\nOver the years, I&#8217;ve messed with the <em>binbin()<\/em> function. Here&#8217;s the current version, which I now call <em>binString()<\/em>:<\/p>\n<pre class=\"screen\">\r\nchar *binString(unsigned short n)\r\n{\r\n    static char bin[17];\r\n    int x;\r\n\r\n    for(x=0;x&lt;16;x++)\r\n    {\r\n        bin[x] = n &amp; 0x8000 ? '1' : '0';\r\n        n &lt;&lt;= 1;\r\n    }\r\n    bin[16] = '\\0';\r\n\r\n    return(bin);\r\n}<\/pre>\n<p>The <em>binString()<\/em> function accepts an <em>unsigned short<\/em> integer as input, variable <code>n<\/code> inside the function. The <em>short<\/em> variable has a bit width of 16, so the string created is 16-characters long. The <code>bin[]<\/code> <em>char<\/em> array holds output string, plus one extra position for the null character, <code>\\0<\/code>, at the end of the string. It&#8217;s a <em>static char<\/em> array, so its value is retained after the function call.<\/p>\n<p>Within the function, a <em>for<\/em> loop processes each bit in value <code>n<\/code>. The binary <code>&amp;<\/code> (AND) operator compares the far left bit with 1 and sets the character &#8216;1&#8217; or &#8216;0&#8217; depending on the bit&#8217;s value. Value <code>n<\/code> is then shifted left one bit position and the loop continues. The result is a string representing the bit values stored in <code>n<\/code>, which is returned from the function.<\/p>\n<p>Here is the full code I use to test the <em>binString()<\/em> function:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nchar *binString(unsigned short n)\r\n{\r\n    static char bin[17];\r\n    int x;\r\n\r\n    for(x=0;x&lt;16;x++)\r\n    {\r\n        bin[x] = n &amp; 0x8000 ? '1' : '0';\r\n        n &lt;&lt;= 1;\r\n    }\r\n    bin[16] = '\\0';\r\n\r\n    return(bin);\r\n}\r\n\r\nint main()\r\n{\r\n    unsigned short value;\r\n\r\n    printf(\"Enter an integer value, 0 to 65535: \");\r\n    scanf(\"%hd\",&amp;value);\r\n\r\n    printf(\"Decimal value is %d\\n\",value);\r\n    printf(\"Octal value is %o\\n\",value);\r\n    printf(\"Hexadecimal value is 0x%X\\n\",value);\r\n    printf(\"Binary value is %s\\n\",binString(value));\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Sample run:<\/p>\n<pre><code>Enter an integer value, 0 to 65535: 43690\r\nDecimal value is 43690\r\nOctal value is 125252\r\nHexadecimal value is 0xAAAA\r\nBinary value is 1010101010101010<\/code><\/pre>\n<p>I use the <em>binbin()<\/em> version of the <em>binString()<\/em> function in my book, <em>Beginning Programming with C For Dummies<\/em>, to demonstrate the various bitwise functions in C. The <em>binString()<\/em> function improves upon it by returning a full string, but the result is the same: a string representation of bits. This complements the natural capability of the C language to output decimal, octal, and hexadecimal.<\/p>\n<p>But what about the other way?<\/p>\n<p>For example, you can input decimal, octal, and hexadecimal values into a C program, but you can&#8217;t input binary.<\/p>\n<p>The following code accepts hexadecimal input:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    unsigned n;\r\n\r\n    printf(\"Type a hex value: \");\r\n    scanf(\"%x\",&amp;n);\r\n    printf(\"You typed %X, which is %d\\n\",n,n);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <code>%x<\/code> directs the <em>scanf()<\/em> function to read hexadecimal values. Here&#8217;s the sample run:<\/p>\n<pre><code>Type a hex value: AA\r\nYou typed AA, which is 170<\/code><\/pre>\n<p>You could change the <code>%x<\/code> in the <em>scanf()<\/em> function to <code>%o<\/code> to input octal values or <code>%d<\/code> to input decimal values. What you cannot do, however, is use a <code>%<\/code> placeholder to read in a binary value. That&#8217;s a topic I tackle in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=3045\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Another look at my <em>binbin()<\/em> function, which displays a value as a binary string. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3032\">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-3032","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\/3032","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=3032"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3032\/revisions"}],"predecessor-version":[{"id":3062,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3032\/revisions\/3062"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3032"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3032"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3032"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}