How to find a basis which is guaranteed to need 9 or less characters to represent a 12 digits number?
The largest 12 digit number in base 10 is $10^{12} - 1$.  In general the largest $n$ position number in a base $b$ is $b^{n} - 1$.  So in your case you need a base large enough that $b^{9} - 1 &gt; 999,999,999,999$ $(10^{12} - 1).$  Solving for $b$:

$$b^{9} - 1 &gt; 10^{12} - 1$$
$$b^{9} &gt; 10^{12}$$
$$b^{9/9} &gt; 10^{12/9}$$
$$b &gt; 10^{12/9}$$
$$b &gt; \sqrt[9]{10^{12}}$$
$$b &gt; 21.54$$

So make your base 22 and your numeric data will fit in 9 positions.