{"id":7067,"date":"2025-07-19T00:01:27","date_gmt":"2025-07-19T07:01:27","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7067"},"modified":"2025-07-12T14:45:31","modified_gmt":"2025-07-12T21:45:31","slug":"from-base-36-to-decimal","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7067","title":{"rendered":"From Base 36 to Decimal"},"content":{"rendered":"<p>Continuing my Base 36 series, from <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7058\">last week&#8217;s Lesson<\/a>, the <em>base35_string()<\/em> function successfully converts a decimal value into its base 36 representation. To verify that the conversion works, another function is necessary to convert base 36 strings into their decimal equivalents.<br \/>\n<!--more--><br \/>\nConverting bases requires a powers table. In the last two Lessons, the base 36 powers table is built in the <em>main()<\/em> function. But to convert both to and from base 36, the powers table must be available to multiple functions. Therefore, my first task in writing a base-36-to-decimal function is to write a function that provides the base 36 powers to other functions in the code.<\/p>\n<p>The <em>base36_powers()<\/em> function builds the powers table and returns its address:<\/p>\n<pre class=\"screen\">\r\n<span class=\"comments\">\/* build the base 36 powers table *\/<\/span>\r\nlong *base36_powers(void)\r\n{\r\n    static long b36_powers[SIZE];\r\n    int x;\r\n\r\n    for( x=0; x&lt;SIZE; x++ )\r\n        b36_powers[x] = x ? b36_powers[x-1]*BASE36_MAX : 1;\r\n\r\n    return(b36_powers);\r\n}<\/pre>\n<p>This code is pulled from the <em>main()<\/em> function in previous Lessons, though the <code>b36_powers[]<\/code> array is declared <em>static<\/em> and its address returned. I suppose I could also hard code the values as an external array (set in a base 36 header file, for example) and call it good. But I chose to code the <em>base36_powers()<\/em> function instead.<\/p>\n<p>With the powers table available, the next task is to create a <em>b36_decimal()<\/em> function to translate a base 36 string into its decimal value. Here&#8217;s the code:<\/p>\n<pre class=\"screen\">\r\n<span class=\"comments\">\/* return decimal value for base 36 string v *\/<\/span>\r\nlong b36_decimal(char *v)\r\n{\r\n    long *powers,result;\r\n    int x,y,digit;\r\n\r\n    <span class=\"comments\">\/* obtain the powers table *\/<\/span>\r\n    powers = base36_powers();\r\n\r\n    <span class=\"comments\">\/* must process the string backwards *\/<\/span>\r\n    x = 0;\r\n    while( *(v+x) != '\\0' )        <span class=\"comments\">\/* locate the string's end *\/<\/span>\r\n        x++;\r\n    x--;                        <span class=\"comments\">\/* backup to 1's position *\/<\/span>\r\n\r\n    <span class=\"comments\">\/* process the digits (x) and powers (y) *\/<\/span>\r\n    result = 0;\r\n    y = 0;\r\n    while(x&gt;=0)\r\n    {\r\n        <span class=\"comments\">\/* translate digit *\/<\/span>\r\n        if( *(v+x)&gt;='0' &amp;&amp; *(v+x)&lt;='9' )\r\n            digit = *(v+x) - '0';\r\n        else\r\n            digit = *(v+x) - 'A' + 10;\r\n        <span class=\"comments\">\/* fetch power value *\/<\/span>\r\n        result += digit * *(powers+y);\r\n        x--;\r\n        y++;\r\n    }\r\n\r\n    return(result);\r\n}<\/pre>\n<p>After obtaining the powers table, the <em>b36_decimal()<\/em> function must process the incoming string <em>backwards<\/em>. A <em>while<\/em> loop locates the null character at the end of the string, setting this offset in variable <code>x<\/code>.<\/p>\n<p>A second <em>while<\/em> loop works through the string in reverse order, but also uses variable <code>y<\/code> to process the powers table in forward order (starting with element zero). Digits are read and converted from characters <code>'0'<\/code> through <code>'9'<\/code> and <code>'A'<\/code> through <code>'Z'<\/code>. The result is multiplied by the proper power: <code>result += digit * *(powers+y);<\/code><\/p>\n<p>The value in variable <em>result<\/em> is returned.<\/p>\n<p><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_07_19-Lesson.c\" rel=\"noopener\" target=\"_blank\">Click here<\/a> to view the full code on GitHub. The <em>main()<\/em> function prompts for a value, which is translated into base 36 and then translated back. Here&#8217;s a sample run:<\/p>\n<p><code>Enter base 10 value: 12345<br \/>\n12345 in base 36 is 9IX<br \/>\n9IX in decimal is 12345<\/code><\/p>\n<p>With these functions in place, it&#8217;s possible to do anything in base 36: Just perform the calculation in decimal and use the <em>b36_decimal()<\/em> and  <em>base36_string()<\/em> functions to translate the results. I was originally going to code a slew of math functions that operated in base 36, but found the task to be silly given that all you really need to do is translate the results. After all, all calculations in a computer are done in base 2 anyway. Anything else you see is just for show.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Base 36 values look weird to humans, so a function is needed to translate these values to decimal. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7067\">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-7067","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\/7067","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=7067"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7067\/revisions"}],"predecessor-version":[{"id":7076,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7067\/revisions\/7076"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7067"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7067"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7067"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}