Just as Babylonians wisely counted in sexagesimal, the wise web developer is naturally drawn to hexahexacontadecimal.
With an alphabet consisting of all unreserved URL characters:
0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_.~
we get a radix of 66, the densest possible way to express a number in a URL.
In hexahexacontadecimal the unsightly number 302 231 454 903 657 293 676 544 turns into the sleek i FsG UkO .0t sxw
. Behold, the pinnacle of URL numeral systems.
def hexahexacontadecimal_encode_int(n):
"""Represent a number in hexahexacontadecimal."""
alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_.~"
if n == 0:
return alphabet[0]
r = StringIO()
while n > 0:
n, t = divmod(n, 66)
r.write(alphabet[t])
return r.getvalue()[::-1]
(You could have called this base66 or base 66 but why would you?)
Update: hexahexacontadecimal is now available as a Python package.