0

I am encountering erroneous results while trying to encode and decode a string read from a file on disk.

I am using the following code to decode and then encode :

byte[] word_bytes = new BASE64Decoder().decodeBuffer(word); //word is string variable

String tempStr = new BASE64Encoder().encode(word_bytes);

On the basis of this code, I was expecting that if I print "word" and "tempStr", I should see the same result. However, the values are different.

For example, if word = "hi!", then tempStr = "hi//" and all single letter words get converted to null values. For example, if word = "a", then tempStr = "".

The file on disk is in UTF-8 encoding.

It would be very helpful if someone can explain the cause of this error.

sorry if this is very basic question, I don't have much prior experience with encoding formats.

Thanks

1 Answer 1

1

You have your encoding/decoding backwards

Base64 is a way to turn a byte array into a String that can easily be passed around without messing up the byte encoding.

Therefore you encode a byte array into a String and then decode it back into a byte array

If you switch the order of your encoding and decoding

     String tempStr = new BASE64Encoder().encode("hi".getBytes());

     byte[] word_bytes = new BASE64Decoder().decodeBuffer(tempStr);

     System.out.println(tempStr);
     System.out.println(new String(word_bytes));

It will print this:

aGk=
hi

It also appears you are using the sun Base64 classes which is not a good idea. Those are internal classes and there is no guarantee that they will be around in a future version of java or that they are present in all Java implementations.

If you are using Java 8, you can use the new java.util.Base64 class javadoc here and if you are on pre Java 8 you should use a 3rd party library which has Base64 classes instead.

Sign up to request clarification or add additional context in comments.

2 Comments

hi.getBytes() and new String(word_bytes) should really be hi.getBytes("utf-8") and new String(word_bytes, "utf-8") (or choose some other encoding). otherwise the behavior is dependent on the default character encoding of the system.
thanks dkatzel! understood the issue, its working fine fore me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.