Java can handle a radix as high as 36 using the digits 0 - 9 and lower case letters a - z.
> Long.toString(12345678901L, 36)
"5o6aqt1"
> Long.parseLong("5o6aqt1", 36)
12345678901
You could create your own encoding using 65 of the 66 unreserved URI Characters (so your URI would not need escaping). The '-' sign needs to be used for negative numbers:
> Long65.toString(12345678901L)
"aFDIbA"
> Long65.parseLong65("aFDIbA")
12345678901
Here is the code for Long65()
import java.math.BigInteger;
public class Long65 {
private static int base = 65;
private static String URIchars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_.~";
public static String toString(Long aNumber) {
StringBuilder result = new StringBuilder();
if (aNumber < 0) {
result.append('-');
aNumber = -aNumber;
}
int r = (int)(aNumber % base);
if (aNumber - r == 0)
result.append(URIchars.charAt(r));
else
result.append(Long65.toString((aNumber - r) / base) + URIchars.charAt(r));
return result.toString();
}
public static long parseLong65(String aNumber) {
char[] digits;
int sign = 1;
if (aNumber.charAt(0) == '-') {
sign = -1;
digits = aNumber.substring(1).toCharArray();
} else {
digits = aNumber.toCharArray();
}
BigInteger bigBase = BigInteger.valueOf(base);
BigInteger power = bigBase.pow(digits.length);
BigInteger total = BigInteger.valueOf(0);
for (char digit : digits){
power = power.divide(bigBase);
total = total.add(power.multiply(BigInteger.valueOf(URIchars.indexOf(digit))));
}
return sign * total.longValue();
}
}