Reputation: 197
Upvotes: 1
Views: 3109
Reputation: 328737
The original answer proposed to use BigInteger with a radix of 36, but that will not be enough for 19 digits.
I don't know if there are libraries to convert to/from base 62, but the contrived example below gives you an idea of how you could do it. The output is:
originalId = 999999999999999999
newId = bUI6zOLZTrh
retrieveOriginalId = 999999999999999999
The rationale of using base 62 is as follows:
N ^ 11
11-char numbers in base N (for example, in base 10, an 11-digit number can be between 0 and 10 ^ 11 or 100 billion)Sample code (algorithms inspired from BigInteger and Long classes - exception handling to be added):
class Base62 {
private static final BigInteger RADIX = BigInteger.valueOf(62);
private static final char[] DIGITS = {
'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',
'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'
};
public static void main(String[] args) throws IOException {
String originalId = "999999999999999999";
System.out.println("originalId = " + originalId);
String newId = getBase62From10(originalId);
System.out.println("newId = " + newId);
String retrieveOriginalId = getBase10From62(newId);
System.out.println("retrieveOriginalId = " + retrieveOriginalId);
}
/**
*
* @param number a positive number in base 10
*
* @return the same number, in base 62
*/
public static String getBase62From10(String number) {
char[] buf = new char[number.length()];
int charPos = number.length() - 1;
BigInteger i = new BigInteger(number);
BigInteger radix = BigInteger.valueOf(62);
while (i.compareTo(radix) >= 0) {
buf[charPos--] = DIGITS[i.mod(radix).intValue()];
i = i.divide(radix);
}
buf[charPos] = DIGITS[i.intValue()];
return new String(buf, charPos, (number.length() - charPos));
}
/**
*
* @param number a positive number in base 62
*
* @return the same number, in base 10
*/
public static String getBase10From62(String number) {
BigInteger value = BigInteger.ZERO;
for (char c : number.toCharArray()) {
value = value.multiply(RADIX);
if ('0' <= c && c <= '9') {
value = value.add(BigInteger.valueOf(c - '0'));
}
if ('a' <= c && c <= 'z') {
value = value.add(BigInteger.valueOf(c - 'a' + 10));
}
if ('A' <= c && c <= 'Z') {
value = value.add(BigInteger.valueOf(c - 'A' + 36));
}
}
return value.toString();
}
}
Upvotes: 0
Reputation: 40056
One way you can try.
Make your 12-digit to binary presentation, which should be able to represented by 5-bytes. Use base64 to encode it and it should be able to be represented by 9 alpha-numeric character. (ok... base64 did contains several non-alpha-numeric char... :P )
(If you have difficulties making it a 5-byte representation, breaking that 12 digits to 3 groups of 4 digits, each represent by 2 bytes should work too)
Search for base64 and get some understanding about it, then you may implement your own encoding method in similar manner.
Adding some code: (not tested, just give u an idea on it looks like)
String originalId= "123456789012";
String resultString = new String(Base64.encodeBase64(new BigInteger(originalId).toByteArray());
Upvotes: 1