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.
Here is a method, in a simple test program. This method allows any String to represent the digits for the result. As the initial print shows, 62 digits should be sufficient to cover all 7 decimal digit numbers with no more than a 4 character output, so I recommend the decimal digits, lower case alpha and upper case alpha for the 7 digit case.
To cover 9 decimal digits in four encoded digits you would need at least 178 characters, which is not possible using only the 7-bit ASCII characters. You would have to decide which additional characters to use as digits.
Output: