This month’s Exercise output values in various bases, but only those bases from 2 to 10. For other bases, especially those that alien beings might use, more effort is required. Therefore, I’d like to begin exploring a ridiculous counting base, base 36.
I tried to devise a clever name for base 36. I wanted to call it yardle because 36 inches are in a yard, but this word is already used in various capacities. So, my efforts are expressed as “base 36.” Boring.
Base 36 builds on the technique used in Hexadecimal, base 16. To express values 11 through 15 as single digits, letters A through F are used:
0 1 2 3 4 5 6 7 8 9 A B C D E F
To represent 36 values in a single digit, I continue this pattern on up to letter Z:
0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
These characters allow for values up to 36 in each position, which makes for larger numbers to be expressed with fewer digits. It also allows for the base program I wrote to be updated to convert values into any base from 2 up to 36. But this type of conversion must happen one step at a time.
The first step is to write a function that outputs a value in base 36. I’m starting small, using only values in the range of zero through 35. In fact, for this code and for future lessons, I’ve set the constant BASE36_MAX
equal to 36. Here’s the code:
2025_06_28-Lesson.c
#include <stdio.h>
#define BASE36_MAX 36
/* output a base 36 value */
char base36_output(int v)
{
if( v<10 )
return( v+'0' );
else if( v<BASE36_MAX )
return( v+'A'-10 );
else
return('\0');
}
int main()
{
int x;
puts("Decimal - Base 36");
for( x=0; x<BASE36_MAX; x++ )
printf("%2d\t%c\n",x,base36_output(x) );
return 0;
}
The base36_output() function accepts a value, v
. This value is converted into the base 36 character, 0 through Z, and the character is returned. Invalid input returns the null character.
The main() function calls base36_output() 36 times, displaying the decimal and base 36 equivalents as output:
Decimal - Base 36
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 A
11 B
12 C
13 D
14 E
15 F
16 G
17 H
18 I
19 J
20 K
21 L
22 M
23 N
24 O
25 P
26 Q
27 R
28 S
29 T
30 U
31 V
32 W
33 X
34 Y
35 Z
The next step in this program’s evolution is to express larger values in base 36 notation. This improvement to the code is presented starting with next week’s Lesson. Then, providing that I can get everything to work, future Lessons play with base 36 math. Fun!
you are definitely stalking what I’m having struggles with … 😉
In https://stackoverflow.com/questions/111928/is-there-a-printf-converter-to-print-in-binary-format/34641674 “chux” proposed a variable base integer to string converter, TO_BASE, we found it a little weak in that the return value invalidates soon, trying to expand to “TO_BASEL” for 64-bit and “TO_BASELL” for 128-bit integers, running them in a benchmarking loop, and compiling with different optimizations led to various instabilities … confusing …
Aside from that I see other bases not very promising, 10 is user common, 2 is computer native, 4, 8, 16, 32, 64 are shorthand notations for base 2, and the others combine being neither human nor computer compatible … “fraction math” would be superior to decimal and to binary, would provide “arithmetic”, alas not very easy in machines, and not liked by humans …
I appreciate that you’re trying to find a practical application for other bases. I’m just being weird. Plus, it’s fun to program.
Base 64? I like it.
Are you familiar with the GMP library? https://c-for-dummies.com/blog/?p=5532
Base 64 is quite useful, its best known use being to embed image data in ASCII text. Unfortunately it’s not just a continuation of hexadecimal because it starts with letters (0 = A etc.) so can’t be incorporated into general purpose code.