{"id":86,"date":"2014-05-02T09:59:07","date_gmt":"2014-05-02T09:59:07","guid":{"rendered":"http:\/\/tinker.eccentrify.com\/?p=86"},"modified":"2014-05-02T09:59:07","modified_gmt":"2014-05-02T09:59:07","slug":"7-guide-to-ascii-char-and-printing","status":"publish","type":"post","link":"https:\/\/tinker.eccentrify.com\/?p=86","title":{"rendered":"$7 guide to ASCII, char, and printing."},"content":{"rendered":"<p>So. I know Nick has thrown a bunch of ASCII and serial stuff at you guys,<br \/>\nbut I don&#8217;t know if anyone has put it together for you with the binary<br \/>\nstuff in Luke&#8217;s handout from the start of semester. So this is a $7<br \/>\nguide to binary encoding, which I will develop into ASCII and printing in C.<\/p>\n<p>Firstly, let&#8217;s get some bullshit out of the way. People say computers<br \/>\nonly understand 1s and 0s. This is, of course, ridiculous. Computers<br \/>\ndon&#8217;t understand anything. They can store 1s and 0s like a<br \/>\nmotherfucker, but they don&#8217;t have the faintest idea what they &#8220;mean&#8221;.<\/p>\n<p>That&#8217;s because what they &#8220;mean&#8221; is entirely dependent on context. The<br \/>\n8 bits 1011 0011 don&#8217;t &#8220;mean&#8221; 227. They don&#8217;t &#8220;mean&#8221; -77, either, for<br \/>\nthose who are up to speed with 2&#8217;s complement. They might be 8 bits<br \/>\nfrom the middle of a longer number, or a decimal number. Who knows?<\/p>\n<p>So the computer stores 1s and 0s, and we assign meaning to that<br \/>\ndepending on context. They are stored in chunks of 8 bits because<br \/>\nthat&#8217;s a nice round number if you&#8217;re working in binary. People have<br \/>\nplayed with 7 bits and 9 bits, or 10 bits, but these days 8 is<br \/>\nstandard. Note again, though, that it&#8217;s a chosen standard &#8211; 8 bits to a<br \/>\nbyte is not actually any inherent limit.<\/p>\n<p>One of the things humans really like doing with computers, it turns out, is<br \/>\ntyping and printing words. And the thing about words is that they have<br \/>\nletters. So how do we store words as ones and zeros? Well, we can<br \/>\npretty easily see that letters map to numbers. There are 26 letters in<br \/>\nthe alphabet. But when we print them, we need a different number to<br \/>\nsay &#8220;this is a capital&#8217;. So we need 52 numbers. Plus some punctuation.<br \/>\nTurns out we need about a hundred characters to pretty much cover<br \/>\nwritten English. Then, of course, we&#8217;re printing this on a screen &#8211; we<br \/>\nmight want some &#8220;characters&#8221; to tell the screen what to do (&#8216;tab&#8217;, for<br \/>\nexample, to shortcut printing 4 spaces, or &#8220;new line&#8221;).<\/p>\n<p>We can still fit all that handily in 128 characters. That&#8217;s less than<br \/>\na byte, so let&#8217;s round up and say we&#8217;ll store one character per byte.<br \/>\nThere are technical reasons for that, but basically, it&#8217;s as easy to<br \/>\nget a full byte from memory as it is to get half a byte (or a nybble,<br \/>\nif you prefer), and it&#8217;s easier to convert a byte to a number than to<br \/>\nsplit it in half and convert each 4 bits into a number. So now if we<br \/>\nhave some standard way to assign numbers to letters, we can just store<br \/>\nthose numbers, and then when we retrieve them from memory, we can look<br \/>\nup the letter, and print that. So back in the day, a bunch of<br \/>\nboffins got together and agreed on a standard for converting letters<br \/>\nto numbers. Actually, because this is computing, 2 separate bunches of<br \/>\nguys agreed on 2 separate standards, and then the market decided. So<br \/>\nwhat we have now is ASCII &#8211; the American Standard Code for Information<br \/>\nInterchange. Because EBCDIC was even worse.<\/p>\n<p>Which brings me to C. I&#8217;ve been talking about storing and printing,<br \/>\nand perhaps rather than moving on to serial transmission, I&#8217;ll look at<br \/>\nhow that actually works, and what the implications are.<\/p>\n<p>Let&#8217;s start with the following:<\/p>\n<blockquote><p>#include &lt;stdio.h&gt;<br \/>\nint main(void){<br \/>\nchar Alice = &#8216;A&#8217;;<br \/>\nchar Bob = 66;<br \/>\nprintf(&#8220;%c\\n&#8221;, Alice);<br \/>\nprintf(&#8220;%c\\n&#8221;, Bob);<br \/>\n}<\/p><\/blockquote>\n<p>The next $7 guide will look at why main has int and void, because that<br \/>\nseems to be a popular question, but for now, focus on what&#8217;s in main.<\/p>\n<p>So we say to The Machine: &#8220;I would like enough memory to store a character,<br \/>\nand I will call that chunk of memory Alice. In that chunk of memory,<br \/>\nplease store the value of capital A.&#8221;<\/p>\n<p>The single quotes around &#8216;A&#8217; are important. If we just wrote<br \/>\nchar Alice = A;<\/p>\n<p>then The Machine would think we had another chunk of memory<br \/>\nsomewhere called A, and get confused because it couldn&#8217;t find it.<br \/>\nSo the single quotes say &#8220;Store the literal thing inside these<br \/>\nquotes&#8221;. The Machine is just clever enough to know that it can&#8217;t store<br \/>\n&#8216;A&#8217;, so it goes away and looks up the ASCII table and gets a number<br \/>\nfor A. That will be 65.<\/p>\n<p>Reasonably enough, capital B is the letter after A. Normally, it&#8217;s<br \/>\neasier to put &#8216;B&#8217; and make The Machine look it up, but this<br \/>\ndemonstrates that we can skip that step and put a number in directly.<br \/>\nNote that we don&#8217;t need quotes around numbers. The Machine is smrt<br \/>\nenough to recognise a number.<\/p>\n<p>[Bonus points: digits have ASCII codes that don&#8217;t match the &#8220;number&#8221;<br \/>\nrepresented by the digit. If you have &#8220;Alice = &#8216;1&#8217;;&#8221;, then the quotes<br \/>\nsay &#8220;store the value of the thing in the quotes&#8221;, and the ASCII value<br \/>\nfor the digit &#8220;1&#8221; is 49. Yay?]<\/p>\n<p>So now we move on to printing those. printf() is an amazingly clever<br \/>\nroutine. We put what we want to print in double quotes this time, and<br \/>\nwe say to printf &#8220;I want you to print a character here&#8221;. That&#8217;s the<br \/>\n%c. %c just says &#8221; I will give you a number. Go look the number up in<br \/>\nthe ASCII table, and print the letter that matches.&#8221;<\/p>\n<p>Of course, we have lied to poor printf &#8211; what we actually give it is the<br \/>\nname of a chunk of memory called Alice. But printf can just about<br \/>\nmanage to pull a number out of that, and that number is 65, which<br \/>\nmatches A.<\/p>\n<p>Likewise B.<\/p>\n<p>So this is all very fascinating, and I know you&#8217;re all thrilled, but<br \/>\nthis is a hell of a bg deal to be making out of looking up letters.<br \/>\nTurns out, of course, that it&#8217;s more interesting than that. Try this:<\/p>\n<blockquote><p>#include &lt;stdio.h&gt;<\/p>\n<p>int main(void){<br \/>\nchar Alice = 65;<br \/>\nchar Bob = Alice +1;<br \/>\nchar Zarathustra = Alice +25;<br \/>\nchar Littlebob = Bob + 32;<br \/>\nchar Zero = 0x30;<\/p>\n<p>printf(&#8220;%c %d \\n&#8221;, Alice, Alice);<br \/>\nprintf(&#8220;%c %d \\n&#8221;, Bob, Bob);<br \/>\nprintf(&#8220;%c %d \\n&#8221;, Zarathustra, Zarathustra);<br \/>\nprintf(&#8220;%c %d \\n&#8221;, Littlebob, Littlebob);<br \/>\nprintf(&#8220;%c %d \\n&#8221;, Zero, Zero);<br \/>\n}<\/p><\/blockquote>\n<p>Yow. Mind. Blown. Now you see why this is the most exciting way I<br \/>\ncould think of to spend my Friday night.<\/p>\n<p>Lookit:<\/p>\n<blockquote><p>#include &lt;stdio.h&gt;<br \/>\nint main(void){<br \/>\nint i\u00a0 = 0;<\/p>\n<p>for ( i = 0; i &lt; 10; i++){<br \/>\nprintf(&#8220;%c \\n&#8221;, 48 + i);<br \/>\n}<\/p>\n<p>for ( i = 1; i&lt;27; i++){<br \/>\nint Alice = 64 +i;<br \/>\nprintf(&#8221; Big %c ! little %c \\n&#8221;,Alice , Alice + 32 );<br \/>\n}<\/p>\n<p>}<\/p><\/blockquote>\n<p>Now we&#8217;ve made Alice an int. The only difference between a char and an int<br \/>\nis how much memory is put aside. &#8220;char&#8221; says &#8220;give me one byte&#8221;. &#8220;int&#8221; says<br \/>\n&#8220;give me enough memory to store an int&#8221;. That s technically dependent on the<br \/>\nsystem, but for most desktops is 4 bytes. So I&#8217;m using more memory, but<br \/>\nsince a &#8220;char&#8221; is just &#8220;a number that fits in 1 byte&#8221;, it also fits in 4<br \/>\nbytes. And since it&#8217;s just a number, there&#8217;s no difference between 01000001 and<br \/>\n00000000 00000000 00000000 01000001.<\/p>\n<p>And if that&#8217;s not exciting enough:<\/p>\n<blockquote><p>#include &lt;stdio.h&gt;<br \/>\nint main(void){<br \/>\n  int i\u00a0 = 0;<br \/>\n  for ( i =0; i&lt;26; i++){<br \/>\n    printf(&#8221; Big %c ! little %c \\n&#8221;, &#8216;A&#8217; +i , &#8216;A&#8217; + i + 32 );<br \/>\n  }<br \/>\n}<\/p><\/blockquote>\n<p>The value of &#8216;A&#8217; is a number, and we can add it to other numbers and do math<br \/>\nwith it directly.<\/p>\n<p>Now, if you&#8217;ll excuse me, I think I need a cold shower. Any questions?<\/p>\n<p>There are 2 more $7 guides in the works. One of them addresses why &#8220;main()&#8221;<br \/>\nworks even though you should write &#8220;int main(void)&#8221; anyway (or perhaps why<br \/>\nyou should write &#8220;int main(void)&#8221; when &#8220;main()&#8221; works just as well. That will<br \/>\ninclude what the hell &#8220;(int argc, char *argv[])&#8221; means, and probably also a<br \/>\nrant about why the Pugh book has issues in the 21st century.<\/p>\n<p>The other one will be on serial transmission, honest, and how to know when<br \/>\n01000110 01110101 01100011 01101011 means a very large number, and when it<br \/>\nmeans a very rude word (short answer, you can&#8217;t. Longer answer, it&#8217;s up to you!)<br \/>\nBut I&#8217;m open to suggestions if anyone has requests.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So. I know Nick has thrown a bunch of ASCII and serial stuff at you guys, but I don&#8217;t know if anyone has put it together for you with the binary stuff in Luke&#8217;s handout from the start of semester. So this is a $7 guide to binary encoding, which I will develop into ASCII [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-86","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/tinker.eccentrify.com\/index.php?rest_route=\/wp\/v2\/posts\/86","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tinker.eccentrify.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tinker.eccentrify.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tinker.eccentrify.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/tinker.eccentrify.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=86"}],"version-history":[{"count":10,"href":"https:\/\/tinker.eccentrify.com\/index.php?rest_route=\/wp\/v2\/posts\/86\/revisions"}],"predecessor-version":[{"id":103,"href":"https:\/\/tinker.eccentrify.com\/index.php?rest_route=\/wp\/v2\/posts\/86\/revisions\/103"}],"wp:attachment":[{"href":"https:\/\/tinker.eccentrify.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=86"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tinker.eccentrify.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=86"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tinker.eccentrify.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=86"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}