{"id":7058,"date":"2025-07-12T00:01:47","date_gmt":"2025-07-12T07:01:47","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7058"},"modified":"2025-07-19T08:29:56","modified_gmt":"2025-07-19T15:29:56","slug":"from-decimal-to-base-36","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7058","title":{"rendered":"From Decimal to Base 36"},"content":{"rendered":"<p>The next step in my Base 36 series is to translate a decimal value into its base 36 representation. In <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7043\">last week&#8217;s Lesson<\/a>, code was presented to build a powers table and slice a decimal value into its base 36 components. This Lesson completes the task with a function, <em>base36_sting()<\/em> to build and output a base 36 value.<br \/>\n<!--more--><br \/>\nTo craft a base 36 number, the various powers of 36 must be stored in an array based on the value translated. For example, for the value 12,345 the base 36 value is 9IX, as shown in Figure 1.<\/p>\n<div id=\"attachment_7054\" style=\"width: 510px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-7054\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2025\/07\/0705-figure2.png\" alt=\"\" width=\"500\" height=\"286\" class=\"size-full wp-image-7054\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2025\/07\/0705-figure2.png 500w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2025\/07\/0705-figure2-300x172.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><p id=\"caption-attachment-7054\" class=\"wp-caption-text\">Figure 1. How the base 36 value 9IX translates into decimal.<\/p><\/div>\n<p>This new array stores values for their power positions, one element for each power of 36:<\/p>\n<p>9 in the 1,269 (36<sup>2<\/sup>) position<br \/>\nI in the 36 (36(<sup>1<\/sup>) position<br \/>\nX in the 1 (36<sup>0<\/sup>) position.<\/p>\n<p>Of course, this array holds values, not the base 36 characters:<\/p>\n<p><code>b36_values[2]<\/code> = 11,664 (9 &times; 1,269)<br \/>\n<code>b36_values[1]<\/code> = 648 (I &times; 36)<br \/>\n<code>b36_values[0]<\/code> = 33 (X &times; 1)<\/p>\n<p>The <em>base36_string()<\/em> function translates these values into characters, 0 to 9 and A to Z. Here is my code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_07_12-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2025_07_12-Lesson-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\n#define BASE36_MAX 36\r\n#define SIZE 10\r\n\r\n<span class=\"comments\">\/* generate a base 36 value *\/<\/span>\r\nchar *base36_string(int *v)\r\n{\r\n    static char b36_string[SIZE+1];\r\n    int x,y;\r\n\r\n    <span class=\"comments\">\/* build base 36 string *\/<\/span>\r\n    y = SIZE-1;\r\n    for( x=0; x&lt;SIZE-1; x++ )\r\n    {\r\n        if( *(v+y)&lt;10 )\r\n            b36_string[x] = *(v+y)+'0';\r\n        else\r\n            b36_string[x] = *(v+y)+'A'-10;\r\n        y--;\r\n    }\r\n    b36_string[x] = '\\0';\r\n\r\n    return(b36_string);\r\n}\r\n\r\nint main()\r\n{\r\n    long b36_powers[SIZE];\r\n    int b36_values[SIZE];\r\n    int x,d;\r\n\r\n    <span class=\"comments\">\/* prompt for input *\/<\/span>\r\n    printf(\"Enter base 10 value: \");\r\n    scanf(\"%d\",&amp;d);\r\n    if( d&lt;1 )\r\n    {\r\n        puts(\"Enter a value larger than zero\");\r\n        return 1;\r\n    }\r\n\r\n    <span class=\"comments\">\/* build the powers table *\/<\/span>\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    <span class=\"comments\">\/* translate the decimal value *\/<\/span>\r\n    for( x=SIZE-2; x&gt;=0; x-- )\r\n        b36_values[x+1] = ( d % b36_powers[x+1])\/ b36_powers[x];\r\n\r\n    <span class=\"comments\">\/* output the result in base 36 *\/<\/span>\r\n    printf(\"%s\\n\",base36_string(b36_values));\r\n\r\n    <span class=\"comments\">\/* clean-up *\/<\/span>\r\n    return 0;\r\n}<\/pre>\n<p>The <em>main()<\/em> function duplicates some code from last week&#8217;s Lesson, but with an extra array, <code>b36_values[]<\/code>. The second <em>for<\/em> loop fills this array:<\/p>\n<p><code>for( x=SIZE-2; x&gt;=0; x-- )<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;b36_values[x+1] = ( d % b36_powers[x+1])\/ b36_powers[x];<\/code><\/p>\n<p>The array is passed to the <em>base36_string()<\/em> function and output directly in the next <em>printf()<\/em> statement.<\/p>\n<p>In the <em>base36_string()<\/em> function, the array is passed as a pointer, <code>v<\/code>. A <em>for<\/em> loop in the function works through the <em>static char<\/em> <code>string b36_string[]<\/code>. Variable <code>y<\/code> processes the passed array backwards, plucking out powers of 36 and filling the <code>b36_string[]<\/code> array with a base 36 value, 0 through 9 and A through Z. The string is capped with a null character and returned for output.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<p><code>Enter base 10 value: 12345<br \/>\n0000009IX<\/code><\/p>\n<p>The function properly returns a base 36 string &mdash; but the string is padded with leading zeros. I thought of several ways to deal with them.<\/p>\n<p>First, I could allocate the string as a pointer instead of a <em>static char<\/em> array. Then the pointer is returned referencing the first non-zero character in the string. But by allocating a pointer, another problem is created: freeing the pointer. Not only is the pointer no longer referencing its true base, but every time that the function is called more pointers are created. Especially when a base 36 string is used directly, as shown in the code above, freeing it becomes an issue.<\/p>\n<p>Second, I thought of creating another array to return instead, copying the relevant characters into this second array. This solution works, but seems clunky to me.<\/p>\n<p>Third, I thought of shuffling characters in the existing <code>b36_string[]<\/code> array, which is what I ended up doing. You can <a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_07_12-Lesson-b.c\" target=\"_blank\">click here<\/a> to view this addition on GitHub.<\/p>\n<p>In <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7067\">next week&#8217;s Lesson<\/a>, I present a function to translate a base 36 string into its decimal value.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As humans tend to think in base 10, it&#8217;s necessary to write a function that translates base 10 values into base 36. This step will help understand those alien beings who count in base 36. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7058\">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-7058","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\/7058","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=7058"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7058\/revisions"}],"predecessor-version":[{"id":7082,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7058\/revisions\/7082"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7058"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7058"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7058"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}