75

I'm trying to decode a simple Base64 string, but am unable to do so. I'm currently using the org.apache.commons.codec.binary.Base64 package.

The test string I'm using is: abcdefg, encoded using PHP YWJjZGVmZw==.

This is the code I'm currently using:

Base64 decoder = new Base64();
byte[] decodedBytes = decoder.decode("YWJjZGVmZw==");
System.out.println(new String(decodedBytes) + "\n") ;   

The above code does not throw an error, but instead doesn't output the decoded string as expected.

10
  • well, first of all, you should always specify a character set when converting from bytes to chars, perhaps "US-ASCII" for you example string (new String(decodedBytes, "US-ASCII")). Commented Jul 18, 2012 at 15:13
  • @jtahlborn the apache commons codec is built to "just work" in system default encoding if you don't specify otherwise Commented Jul 18, 2012 at 15:18
  • @TomasB code you posted works fine for me, is that the full sample? Commented Jul 18, 2012 at 15:18
  • 2
    @Affe - i have no idea what you mean by it "just working in the system default encoding". regardless of the library used, when you convert bytes to chars, you should always specify a charset. Commented Jul 18, 2012 at 15:22
  • 2
    If it prints nothing, the problem is elsewhere in your program, not with the Base-64 decoding. Commented Jul 18, 2012 at 15:27

4 Answers 4

80

Modify the package you're using:

import org.apache.commons.codec.binary.Base64;

And then use it like this:

byte[] decoded = Base64.decodeBase64("YWJjZGVmZw==");
System.out.println(new String(decoded, "UTF-8") + "\n");
Sign up to request clarification or add additional context in comments.

6 Comments

Sorry I should specified that I have to use org.apache.commons.codec.binary.Base64 because it's a safe url decoder.
There's javax.xml.bind.DatatypeConverter.parseBase64Binary in JDK so you don't need to use sun API or external libraries to work with base64.
@RTB you should always use a characted encoding when constructing a string from a byte[]. Change to e.g. new String(decoded, "UTF-8")
@TomasB If my anwser helped please accept it so others can learn form it. If not how can i help you further?
|
20

If you don't want to use apache, you can use Java8:

byte[] decodedBytes = Base64.getDecoder().decode("YWJjZGVmZw=="); 
System.out.println(new String(decodedBytes) + "\n");

Comments

18

The following should work with the latest version of Apache common codec

byte[] decodedBytes = Base64.getDecoder().decode("YWJjZGVmZw==");
System.out.println(new String(decodedBytes));

and for encoding

byte[] encodedBytes = Base64.getEncoder().encode(decodedBytes);
System.out.println(new String(encodedBytes));

Comments

3

Commonly base64 it is used for images. if you like to decode an image (jpg in this example with org.apache.commons.codec.binary.Base64 package):

byte[] decoded = Base64.decodeBase64(imageJpgInBase64);
FileOutputStream fos = null;
fos = new FileOutputStream("C:\\output\\image.jpg");
fos.write(decoded);
fos.close();

3 Comments

Base64 is a byte[] encoding specification and has nothing specifically to do with images nor is such a practice common.
Base 64 encoding of images is common on older or less sophisticated messaging protocols.
It's common enough to encode images in base64 when embedding them in XML documents or in HTML data URIs.

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.