Hello,
I searched a lot to find a proper base64 encoding and decoding mechanism. I found the below link useful to me:
Convert Between Base 10, Base 62, Base 36, Base 16, Base 8, Base 2 in Java
The thing is that my database tables have keys which are in the below format:
A sample encoded string from DB is: "0003}C".key = uniqueObjectClassName+ Base64 encoded string.
I used the decoding code from the above link:
public static int fromBase64( String base64Number ) { return fromOtherBaseToDecimal( 64, base64Number ); } private static int fromOtherBaseToDecimal( int base, String number ) { int iterator = number.length(); int returnValue = 0; int multiplier = 1; while( iterator > 0 ) { returnValue = returnValue + ( baseDigits.indexOf( number.substring( iterator - 1, iterator ) ) * multiplier ); multiplier = multiplier * base; --iterator; } return returnValue; } public static void main(String[] args) { System.out.println(BaseConverterUtil.fromBase64("0003}C")); }
And the result is: 12236. I checked this in the vendors GUI and confirmed that it is correct. However when I use this String - "12236" and call the encoding API:
public static String toBase64( int decimalNumber ) { return fromDecimalToOtherBase( 64, decimalNumber ); } private static String fromDecimalToOtherBase ( int base, int decimalNumber ) { String tempVal = decimalNumber == 0 ? "0" : ""; int mod = 0; while( decimalNumber != 0 ) { mod = decimalNumber % base; tempVal = baseDigits.substring( mod, mod + 1 ) + tempVal; decimalNumber = decimalNumber / base; } return tempVal; }
I get the below exception:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 64
It should ideally return me: "0003}C". Can any one help ?