{"id":1000,"date":"2014-10-18T00:01:17","date_gmt":"2014-10-18T07:01:17","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1000"},"modified":"2014-10-11T07:40:08","modified_gmt":"2014-10-11T14:40:08","slug":"ascii-programming-tricks-round-i","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1000","title":{"rendered":"ASCII Programming Tricks, Round I"},"content":{"rendered":"<p>The clever organization of ASCII character codes lends itself to some useful programming tricks. One of those tricks involves converting from character codes &#8216;0&#8217; through &#8216;9&#8217; to integer values zero through nine.<br \/>\n<!--more--><br \/>\nFrom your standard, ASCII chart, behold the character codes for &#8216;0&#8217; through &#8216;9&#8217;:<\/p>\n<div align=\"center\">\n<table border=\"1\" cellpadding=\"2\" width=\"50%\">\n<tr>\n<th>Decimal<\/th>\n<th>Hexadecimal<\/th>\n<th>Character<\/th>\n<\/tr>\n<tr>\n<td align=\"right\" width=\"33%\">48<\/td>\n<td align=\"right\" width=\"33%\">0x30<\/td align=\"center\">\n<td>0<\/td>\n<\/tr>\n<tr>\n<td align=\"right\">49<\/td>\n<td align=\"right\">0x31<\/td align=\"center\">\n<td>1<\/td>\n<\/tr>\n<tr>\n<td align=\"right\">50<\/td>\n<td align=\"right\">0x32<\/td align=\"center\">\n<td>2<\/td>\n<\/tr>\n<tr>\n<td align=\"right\">51<\/td>\n<td align=\"right\">0x33<\/td align=\"center\">\n<td>3<\/td>\n<\/tr>\n<tr>\n<td align=\"right\">52<\/td>\n<td align=\"right\">0x34<\/td align=\"center\">\n<td>4<\/td>\n<\/tr>\n<tr>\n<td align=\"right\">53<\/td>\n<td align=\"right\">0x35<\/td align=\"center\">\n<td>5<\/td>\n<\/tr>\n<tr>\n<td align=\"right\">54<\/td>\n<td align=\"right\">0x36<\/td align=\"center\">\n<td>6<\/td>\n<\/tr>\n<tr>\n<td align=\"right\">55<\/td>\n<td align=\"right\">0x37<\/td align=\"center\">\n<td>7<\/td>\n<\/tr>\n<tr>\n<td align=\"right\">56<\/td>\n<td align=\"right\">0x38<\/td align=\"center\">\n<td>8<\/td>\n<\/tr>\n<tr>\n<td align=\"right\">57<\/td>\n<td align=\"right\">0x39<\/td align=\"center\">\n<td>9<\/td>\n<\/tr>\n<\/table>\n<\/div>\n<p>Ignore the Decimal column!<\/p>\n<p>Pay heed to the Hexadecimal column. There you see values 0x30 through 0x39, which neatly parallel the characters &#8216;0&#8217; through &#8216;9&#8217;. In fact, subtract 0x30 from each character code and you get the corresponding value. I think that&#8217;s pretty nifty.<\/p>\n<p>Why is it nifty? Because you can convert from a character code to a value by using simple math in your code.<\/p>\n<p>Yes, I know: The C library features functions that quickly convert text numbers to <em>int<\/em> values. Those functions don&#8217;t help you appreciate the ASCII codes. On the other hand, the following source code might help you understand how to use the ASCII code values as a handy <em>char<\/em>-to-<em>int<\/em> shortcut:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int num;\r\n\r\n    for(num=0x30;num&lt;=0x39;num++)\r\n        printf(\"0x%02X is ASCII '%c' or value %d.\\n\",\r\n                num,\r\n                num,\r\n                num-0x30);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>for<\/em> loop at Line 7 churns through hexadecimal values 0x30 through 0x39, displaying their ASCII code characters, but also the values associated with those characters. The <code>num-0x30<\/code> calculation makes that happen, which could also be expressed as <code>num-'0'<\/code>.<\/p>\n<p>Here&#8217;s the output:<\/p>\n<pre><code>0x30 is ASCII '0' or value 0.\r\n0x31 is ASCII '1' or value 1.\r\n0x32 is ASCII '2' or value 2.\r\n0x33 is ASCII '3' or value 3.\r\n0x34 is ASCII '4' or value 4.\r\n0x35 is ASCII '5' or value 5.\r\n0x36 is ASCII '6' or value 6.\r\n0x37 is ASCII '7' or value 7.\r\n0x38 is ASCII '8' or value 8.\r\n0x39 is ASCII '9' or value 9.<\/code><\/pre>\n<p>Binary logic can instead be used to convert ASCII numeric code values to their true values. That equation would be <code>num & 0x0F<\/code>: The AND mask converts the ASCII codes to their literal values. The expression <code>num - 0x30<\/code>, however, is far more readable.<\/p>\n<p>Obviously, this trick won&#8217;t help you with numeric strings longer than a single digit. For that you need to do more math, multiplying successive ASCII code values by 10 &#8212; but that&#8217;s a thorny problem for another day.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Converting from character &#8216;0&#8217; to value zero. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1000\">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-1000","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\/1000","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=1000"}],"version-history":[{"count":9,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1000\/revisions"}],"predecessor-version":[{"id":1038,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1000\/revisions\/1038"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1000"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1000"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1000"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}