{"id":276,"date":"2018-03-04T04:47:29","date_gmt":"2018-03-04T04:47:29","guid":{"rendered":"http:\/\/loop.cs.mtu.edu\/?p=276"},"modified":"2023-10-31T00:25:53","modified_gmt":"2023-10-31T00:25:53","slug":"ascii-fun","status":"publish","type":"post","link":"https:\/\/loop.cs.mtu.edu\/index.php\/2018\/03\/04\/ascii-fun\/","title":{"rendered":"ASCII Fun"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-279\" src=\"http:\/\/loop.cs.mtu.edu\/wp-content\/uploads\/2018\/03\/ASCII_HiTHERE.jpg\" alt=\"\" width=\"1204\" height=\"258\" srcset=\"https:\/\/loop.cs.mtu.edu\/wp-content\/uploads\/2018\/03\/ASCII_HiTHERE.jpg 1204w, https:\/\/loop.cs.mtu.edu\/wp-content\/uploads\/2018\/03\/ASCII_HiTHERE-300x64.jpg 300w, https:\/\/loop.cs.mtu.edu\/wp-content\/uploads\/2018\/03\/ASCII_HiTHERE-768x165.jpg 768w, https:\/\/loop.cs.mtu.edu\/wp-content\/uploads\/2018\/03\/ASCII_HiTHERE-1024x219.jpg 1024w\" sizes=\"auto, (max-width: 706px) 89vw, (max-width: 767px) 82vw, 740px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">ASCII art like the greeting above is common, but ASCII characters are useful for a lot more. \u00a0Represented in a Java in a manner identical to the first 128 unsigned integers, they can be substituted for ints at any time, and ints 32 -126 inclusive can be cast to chars and printed. \u00a0While limited compared to the unicode set of characters (ASCII can\u2019t represent \u00e1, for example), ASCII is the workhorse of most file systems.<\/span><\/p>\n<p><!--more--><\/p>\n<p><span style=\"font-weight: 400;\">To explore the representation of ASCII a little more let\u2019s look at it on the binary level. \u00a0To a computer, both ints and chars are represented as one byte (8 bits) of 1\u2019s and 0\u2019s, with each 1 or 0 representing 1 bit.<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-280\" src=\"http:\/\/loop.cs.mtu.edu\/wp-content\/uploads\/2018\/03\/ASCII_int0.jpg\" alt=\"\" width=\"879\" height=\"489\" srcset=\"https:\/\/loop.cs.mtu.edu\/wp-content\/uploads\/2018\/03\/ASCII_int0.jpg 879w, https:\/\/loop.cs.mtu.edu\/wp-content\/uploads\/2018\/03\/ASCII_int0-300x167.jpg 300w, https:\/\/loop.cs.mtu.edu\/wp-content\/uploads\/2018\/03\/ASCII_int0-768x427.jpg 768w\" sizes=\"auto, (max-width: 706px) 89vw, (max-width: 767px) 82vw, 740px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-277\" src=\"http:\/\/loop.cs.mtu.edu\/wp-content\/uploads\/2018\/03\/ASCII_a.jpg\" alt=\"\" width=\"978\" height=\"543\" srcset=\"https:\/\/loop.cs.mtu.edu\/wp-content\/uploads\/2018\/03\/ASCII_a.jpg 978w, https:\/\/loop.cs.mtu.edu\/wp-content\/uploads\/2018\/03\/ASCII_a-300x167.jpg 300w, https:\/\/loop.cs.mtu.edu\/wp-content\/uploads\/2018\/03\/ASCII_a-768x426.jpg 768w\" sizes=\"auto, (max-width: 706px) 89vw, (max-width: 767px) 82vw, 740px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">This means that when we cast an int to a char, we\u2019re really just telling the computer \u201cI know a char is 8 bits shorter than an int, but I only want the last (least significant) eight bits.\u201d<\/span><\/p>\n<p><span style=\"font-weight: 400;\">To explore some of the fun things we can do with ASCII characters, let\u2019s make an ASCII viewer program to print them out.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">\/**\n * Prints out all 128 ASCII characters - 63 is authentically the question mark\n *\/\npublic void printAllACSII() {\n      System.out.println(\"All ASCII characters\");\n      for (int i = 0; i &lt; 128; i++) {\n        System.out.print(i + \": \" + (char)i + \" \");\n        \/\/ Print a new line after every five characters\n        if (i &gt; 0 &amp;&amp; i  % 5 == 0) {\n          System.out.println();\n        }\n      }\n      System.out.println();\n}<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">To print out all the ASCII characters, we use a for-loop to step through all integers from 0 to 127 inclusive. \u00a0For each int, we print the int, then cast it to a char and print the char. \u00a0To keep all the characters from printing on a single line, we print a new line if i &gt; 0 and is evenly divisible by 5 (line 32). \u00a0The \u201c&amp;&amp;\u201d is a logical AND, meaning both sides have to be true, for the condition to evaluate to true. \u00a0The modulus operator, \u201c%\u201d, divides the value on its left by the value on its right and returns the remainder. \u00a0At the end, we print out another blank line to move the cursor to a new line. <\/span><\/p>\n<p><span style=\"font-weight: 400;\">Let\u2019s take a look at the results:<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-283\" src=\"http:\/\/loop.cs.mtu.edu\/wp-content\/uploads\/2018\/03\/ASCII_PrintAllResults.jpg\" alt=\"\" width=\"833\" height=\"1080\" srcset=\"https:\/\/loop.cs.mtu.edu\/wp-content\/uploads\/2018\/03\/ASCII_PrintAllResults.jpg 833w, https:\/\/loop.cs.mtu.edu\/wp-content\/uploads\/2018\/03\/ASCII_PrintAllResults-231x300.jpg 231w, https:\/\/loop.cs.mtu.edu\/wp-content\/uploads\/2018\/03\/ASCII_PrintAllResults-768x996.jpg 768w, https:\/\/loop.cs.mtu.edu\/wp-content\/uploads\/2018\/03\/ASCII_PrintAllResults-790x1024.jpg 790w\" sizes=\"auto, (max-width: 706px) 89vw, (max-width: 767px) 82vw, 740px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">What\u2019s going on here? \u00a0Five characters were supposed to print out per line, not two or three. \u00a0And what are all those squiggly question marks in boxes about?<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Both 10 and 13 represent ways to enter to a new line. \u00a0ASCII 10 is the new line character \u201c\\n\u201d, and ASCII 13 is the old carriage return. \u00a0ASCII \u00a09 has a lot of space after it because it is the tab character or \u201c\\t\u201d. \u00a0ASCII 0 prints out a space, the closest representation to its real value, NUL. \u00a0Those question marks inside boxes represent ASCII characters that cannot be displayed. \u00a0Some of them like BEL (ASCII 7) are leftovers from the days of telegraphs and teletype machines. \u00a0Others like DEL (ASCII 127) are simply hard to represent: \u00a0how would you represent a delete character? \u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The displayable ASCII values are only 32 (the space) through 126 (tilde) inclusive. \u00a0We can print them using the method below:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">public void printDisplayableASCII() {\n  System.out.println(\"Displayable ASCII characters\");\n  for(int i = 32; i &lt; 127; i++) {\n    System.out.print(i + \": \" + (char)i + \"\\t\");\n    \/\/ Print a new line after every five characters\n    if ((i - 31) % 5 == 0) {\n      System.out.println();\n    }\n  }\n}<\/pre>\n<p><span style=\"font-weight: 400;\">Print Displayable does roughly the same thing as print all, but only for characters 32 through 126 inclusive. \u00a0Instead of printing just a space after each character, we use the tab (\u201c\\t\u201d)for this one to keep the characters aligned. Also, because we start at 32, we can get the remainder of i &#8211; 31 to determine when to print a new line. \u00a0The results after the for-loop finishes look like this:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Now all the characters printed look familiar.<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-278\" src=\"http:\/\/loop.cs.mtu.edu\/wp-content\/uploads\/2018\/03\/ASCII_displayableResults.jpg\" alt=\"\" width=\"618\" height=\"789\" srcset=\"https:\/\/loop.cs.mtu.edu\/wp-content\/uploads\/2018\/03\/ASCII_displayableResults.jpg 618w, https:\/\/loop.cs.mtu.edu\/wp-content\/uploads\/2018\/03\/ASCII_displayableResults-235x300.jpg 235w\" sizes=\"auto, (max-width: 618px) 100vw, 618px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">We can do a lot more than just print the characters. \u00a0Because they\u2019re represented as integers, we can treat them as such. \u00a0For example, we can add two ASCII characters and return the result as an integer as in the method below:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">public int sumASCII(char ch1, char ch2) {\n  return ch1 + ch2;\n}<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">We can also multiply, subtract, and divide them like normal numbers. \u00a0When we try to print an ASCII character as a char that is out of range, it will display as a question mark (see main below). \u00a0<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">public static void main(String[] args) {\n  ASCII_Viewer self = new ASCII_Viewer();\n  self.printAllACSII();\n  self.printDisplayableASCII();\n  \/* Print a \"character\" that is out of the ASCII range\n   * It displays as a question mark.\n   *\/\n  System.out.println(\"128: \" + (char)128);\n  \n  \/\/ Print out the sum of 'a' + 'A'\n  System.out.println(\"a + A = \" + self.sumASCII('a', 'A'));\n  \n  \/\/ Print out the product of 'a' + 'A'\n  System.out.println(\"a * A = \" + 'a' * 'A');\n}<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">Download the code by clicking on one of the links below, and try seeing what you can do with ASCII characters. \u00a0If you want to see more that can be done, try checking out one of our ciphers, such as the Tradition Caesar Cipher.<\/span><\/p>\n<p>Click here to download:\u00a0\u00a0<span style=\"color: #ff6600;\"><a style=\"color: #ff6600;\" href=\"http:\/\/loop.cs.mtu.edu\/wp-content\/uploads\/2018\/03\/ASCII_Viewer.java\">ASCII_Viewer.java<\/a><\/span><\/p>\n<p><span style=\"font-weight: 400;\">And here to view the code in your web browser:\u00a0 <span style=\"color: #008000;\"><a style=\"color: #008000;\" href=\"http:\/\/loop.cs.mtu.edu\/wp-content\/uploads\/2018\/03\/ASCII_Viewer.txt\">ASCII_Viewer<\/a><\/span><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>ASCII art like the greeting above is common, but ASCII characters are useful for a lot more. \u00a0Represented in a Java in a manner identical to the first 128 unsigned integers, they can be substituted for ints at any time, and ints 32 -126 inclusive can be cast to chars and printed. \u00a0While limited compared &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/loop.cs.mtu.edu\/index.php\/2018\/03\/04\/ascii-fun\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;ASCII Fun&#8221;<\/span><\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,31],"tags":[21,22,23,18],"class_list":["post-276","post","type-post","status-publish","format-standard","hentry","category-easy","category-java","tag-ascii","tag-binary","tag-bits","tag-cryptography"],"_links":{"self":[{"href":"https:\/\/loop.cs.mtu.edu\/index.php\/wp-json\/wp\/v2\/posts\/276","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/loop.cs.mtu.edu\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/loop.cs.mtu.edu\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/loop.cs.mtu.edu\/index.php\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/loop.cs.mtu.edu\/index.php\/wp-json\/wp\/v2\/comments?post=276"}],"version-history":[{"count":5,"href":"https:\/\/loop.cs.mtu.edu\/index.php\/wp-json\/wp\/v2\/posts\/276\/revisions"}],"predecessor-version":[{"id":512,"href":"https:\/\/loop.cs.mtu.edu\/index.php\/wp-json\/wp\/v2\/posts\/276\/revisions\/512"}],"wp:attachment":[{"href":"https:\/\/loop.cs.mtu.edu\/index.php\/wp-json\/wp\/v2\/media?parent=276"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/loop.cs.mtu.edu\/index.php\/wp-json\/wp\/v2\/categories?post=276"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/loop.cs.mtu.edu\/index.php\/wp-json\/wp\/v2\/tags?post=276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}