I have been trying to reduce the length of the way. I represent some integer ID's in my program. For Example
2
3
15
26
63
...
151564852
I would like them to be represented as such (0-9A-Za-z only)
2
3
F
Q
z
...
vDF25a //For example
The approach I thought of is to have 63 if statements which each of the mappings from 0-63 to 0-z respectively and for anything above 64 do a recursion on the value minus 63.
Needless to say, I think my approach is very flawed and impractical. What would be a more appropriate way of doing it?
Update:
Following fge's suggestion I've got the encoder to work correctly, however my decode function only works for up-to length 2 strings, in cases where the string is larger the sum becomes erroneous. For example for 3840 to 3845 this is the output
// Encoded
zw
x
zy
zz
100
// Decoded
3840
3841
3842
3843
124 //Invalid decoding
Here is my code for the decode function
public static int decode(String value)
{
String revStr = new StringBuilder(value).reverse().toString();
int sum = 0;
for (int i=1; i < revStr.length(); i++)
{
for (int j=0; j < ALPHABET.length; j++)
{
if (ALPHABET[j] == revStr.charAt(i))
{
sum += (ALPHABET.length * j) * i;
break;
}
}
}
for (int j=0; j < ALPHABET.length; j++)
{
if (ALPHABET[j] == revStr.charAt(0))
{
sum += j;
break;
}
}
return sum;
}