Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Base64 encoding is not working.

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Base64 encoding is not working.

    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:
    key = uniqueObjectClassName+ Base64 encoded string.
    A sample encoded string from DB is: "0003}C".

    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 ?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Base64 encoding is not working.

    Please post the full text of the error message. It has important info that you left off.

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Base64 encoding is not working.

    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 64
    at java.lang.String.substring(String.java:1935)
    at BaseConverterUtil.fromDecimalToOtherBase(BaseConve rterUtil.java:62)
    at BaseConverterUtil.toBase64(BaseConverterUtil.java: 54)
    at BaseConverterUtil.main(BaseConverterUtil.java:85)
    So line BaseConverterUtil.java:62 [fromDecimalToOtherBase()] is:

    tempVal = baseDigits.substring( mod, mod + 1 ) + tempVal;

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Base64 encoding is not working.

    From the error message I'd say the String has less than 65 characters and the code is trying to access the 65th char (arrays are 0 based so 64 is index of 65th).

    Add some printlns to show the values being used if you can't see what is happening.

  5. #5
    Junior Member
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Base64 encoding is not working.

    Well thats true, thats a plain explanation. But I expected someone to correct the encoding logic.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Base64 encoding is not working.

    I expected someone to correct the encoding logic.
    You need to learn how to do that. We'll try to help you fix your code.

    What is the length of the String that is being called with substring?
    What was the value of base?

  7. #7
    Junior Member
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Base64 encoding is not working.

    Never mind, I fixed it. Thanks.

Similar Threads

  1. Base64 problem
    By FelicianoX in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 16th, 2011, 03:56 AM
  2. Convert file from any encoding to UTF-8
    By efluvio in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: June 26th, 2011, 01:16 AM
  3. Change file encoding from ANSI to UTF-8
    By efluvio in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: June 19th, 2011, 01:48 PM
  4. Problem with Japanese Encoding
    By sendhilpk in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: January 12th, 2010, 02:57 AM
  5. Run Length Encoding Problem
    By Scottj996 in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: January 7th, 2010, 07:24 AM

Tags for this Thread