{"id":1017,"date":"2014-10-25T00:01:54","date_gmt":"2014-10-25T07:01:54","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1017"},"modified":"2014-10-25T07:15:33","modified_gmt":"2014-10-25T14:15:33","slug":"ascii-programming-tricks-round-ii","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1017","title":{"rendered":"ASCII Programming Tricks, Round II"},"content":{"rendered":"<p>You probably don&#8217;t even think about it. How do the <em>toupper()<\/em> and <em>tolower()<\/em> functions do their magic? Both functions take a letter of one case and convert it to the same letter of the opposite case. They don&#8217;t touch any other characters or symbols.<\/p>\n<p>Well, how would you code it?<br \/>\n<!--more--><br \/>\nDon&#8217;t worry; this post isn&#8217;t an Exercise.<\/p>\n<p>If the ASCII table weren&#8217;t laid out as carefully as it is, then converting between uppercase and lowercase would involve a lot of coding overhead. In fact, the conversion process would probably be 26 lines of code, one statement for each letter of the alphabet. Thankfully, that&#8217;s not the case.<\/p>\n<p><em>Ha-ha.<\/em><\/p>\n<p>If you whip out a handy ASCII chart (<a href=\"http:\/\/www.asciitable.com\" target=\"_blank\">ASCII web site<\/a>) or review the code from the <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=981\">ASCII Tricks Lesson<\/a>, you can observe the differences in code values between uppercase and lowercase letters.<\/p>\n<p>The code for letter <code>'A'<\/code> is 65 and for letter <code>'a'<\/code> is 97.<\/p>\n<p>Those are decimal code values, so they&#8217;re unimpressive. Behold the hexadecimal code values:<\/p>\n<p>The hex code for letter <code>'A'<\/code> is 0x41 and for letter <code>'a'<\/code> is 0x61.<\/p>\n<p>That&#8217;s a difference of 0x20 between each letter. The difference holds true for all letters of the alphabet, A to Z: Add 0x20 to <code>'A'<\/code> to get <code>'a'<\/code>; subtract 0x20 from <code>'a'<\/code> to get <code>'A'<\/code>.<\/p>\n<blockquote><p>Oh, and by the way: Comparison-wise, uppercase letters are &#8220;less&#8221; than lowercase. That calculation is based on the character&#8217;s ASCII code value. It&#8217;s also what the <em>strcmp()<\/em> family of functions use to compare two strings.<\/p><\/blockquote>\n<p>Unlike working with ASCII character codes for characters <code>'0'<\/code> through <code>'9'<\/code>, you can&#8217;t just add or subtract 0x20 from an alphabetic character code value without first knowing the character&#8217;s current case. Therefore, I recommend that you instead use binary logic operators to make the conversion. That way, for instance, converting an uppercase letter to uppercase by mistake doesn&#8217;t mess up anything.<\/p>\n<p>To convert a letter to uppercase, you need to remove bit 0x20. The operation is <code>& 0xDF<\/code>, AND 0xDF, illustrated in Figure 1.<\/p>\n<div id=\"attachment_1027\" style=\"width: 460px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1027\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/10\/1025-figure1.png\" alt=\"Figure 1. Converting from &#039;a&#039; to &#039;A&#039; by using the logical &amp; operator.\" width=\"450\" height=\"237\" class=\"size-full wp-image-1027\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/10\/1025-figure1.png 450w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/10\/1025-figure1-300x158.png 300w\" sizes=\"auto, (max-width: 450px) 100vw, 450px\" \/><p id=\"caption-attachment-1027\" class=\"wp-caption-text\">Figure 1. Converting from &#8216;a&#8217; to &#8216;A&#8217; by using the logical &#038; operator.<\/p><\/div>\n<p>To convert a letter to lowercase, you need to set bit 0x20. The operation is <code>| 0x20<\/code> OR 0x20, illustrated in Figure 2.<\/p>\n<div id=\"attachment_1028\" style=\"width: 460px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/10\/1025-figure2.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1028\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/10\/1025-figure2.png\" alt=\"Figure 2. Edit.\" width=\"450\" height=\"245\" class=\"size-full wp-image-1028\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/10\/1025-figure2.png 450w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/10\/1025-figure2-300x163.png 300w\" sizes=\"auto, (max-width: 450px) 100vw, 450px\" \/><\/a><p id=\"caption-attachment-1028\" class=\"wp-caption-text\">Figure 2. Converting from &#8216;A&#8217; to &#8216;a&#8217; by using the logical | operator.<\/p><\/div>\n<p>Refer to my various <em>For Dummies<\/em> C programming books for details on binary logic operators and how they affect values.<\/p>\n<p>Here&#8217;s sample code to demonstrate how the ASCII codes for alphabetic characters can be manipulated between cases:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    char *alphabet = \"ABcdeFGhIJkLmNoPqRstuvWXYz\";\r\n    char *a;\r\n\r\n<span class=\"comments\">\/* Show the setence as-is *\/<\/span>\r\n    printf(\"Default: %s\\n\",alphabet);\r\n\r\n<span class=\"comments\">\/* Convert to all caps *\/<\/span>\r\n    printf(\"  Upper: \");\r\n    a = alphabet;\r\n    while(*a)\r\n    {\r\n        putchar( *a & 0xDF);\r\n        a++;\r\n    }\r\n    putchar('\\n');\r\n\r\n<span class=\"comments\">\/* Conver to l\/c *\/<\/span>\r\n    printf(\"  Lower: \");\r\n    a = alphabet;\r\n    while(*a)\r\n    {\r\n        putchar( *a | 0x20);\r\n        a++;\r\n    }\r\n    putchar('\\n');\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Here is the output:<\/p>\n<pre><code>Default: ABcdeFGhIJkLmNoPqRstuvWXYz\r\n  Upper: ABCDEFGHIJKLMNOPQRSTUVWXYZ\r\n  Lower: abcdefghijklmnopqrstuvwxyz<\/code><\/pre>\n<p>This trick works on all letters of the alphabet, but the logical operators also affect other characters and codes. So if you use this technique, you must first test to determine whether a letter is being manipulated. You can pursue that solution on your own.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Changing letter case is an ASCII trick. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1017\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-1017","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\/1017","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=1017"}],"version-history":[{"count":9,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1017\/revisions"}],"predecessor-version":[{"id":1054,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1017\/revisions\/1054"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1017"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1017"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1017"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}