How to encode a 7 digit integer to a 4 digit string In java?
I have a base36 decoder, which is generating 6 characters,
ex:230150206 is converted to 3T0X1A.
The code for it is as follows:
String f = "230150206";
int d = Integer.parseInt(f.toString());
StringBuffer b36num = new StringBuffer();
do {
b36num.insert(0,(base36(d%36)));
d = d/ 36;
} while (d > 36);
b36num.insert(0,(base36(d)));
System.out.println(b36num.toString());
}
/**
Take a number between 0 and 35 and return the character reprsenting
the number. 0 is 0, 1 is 1, 10 is A, 11 is B... 35 is Z
@param int the number to change to base36
@return Character resprenting number in base36
*/
private static Character base36 (int x) {
if (x == 10)
x = 48;
else if (x < 10)
x = x + 48;
else
x = x + 54;
return new Character((char)x);
}
Can some one share me some other way to achieve this?.
The obtained string can be made in to a substring, but i am looking any other way to do it.