# How to find a basis which is guaranteed to need 9 or less characters to represent a 12 digits number?

I'm trying to map a 12 digit number into a fixed width file. For a number of reasons, it must be compressed in such a way that it is guaranteed to be less than or equal to 9 characters (alpha numeric is fine). My first thought was a change of base, but I can't find an equation which gives an upper bound the number of characters needed for a given base.

For example, transforming into base 32

123456789101 -> 3IV9I6JD

Which is 8 digits. How to find a basis which is guaranteed to need 9 or less characters to represent a 12 digits number?

• Hint: what is the biggest $n$ digit number in base $b$? – Tom van der Zanden Jun 25 '15 at 16:24
• use base-64 instead of base-32 – phuclv May 27 '18 at 9:28

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 > 999,999,999,999$ $(10^{12} - 1).$ Solving for $b$:
$$b^{9} - 1 > 10^{12} - 1$$ $$b^{9} > 10^{12}$$ $$b^{9/9} > 10^{12/9}$$ $$b > 10^{12/9}$$ $$b > \sqrt{10^{12}}$$ $$b > 21.54$$