6

Possible Duplicate:
Decode Base64 data in java

I am working on java profile, I want to know , how to convert the base64 string to byte array. could any one please provide code & it will be help fule to me.

1

2 Answers 2

14

You can also use Apache Commons Codec (http://commons.apache.org/codec/)

String example = "SGVsbG8gV29ybGQ="
byte[] decoded = org.apache.commons.codec.binary.Base64.decodeBase64(example .getBytes());
2
  • Thank you buddy. The Java's stock parser was not working for me. This worked. :) Jun 10, 2015 at 14:06
  • 1
    Since Java 8 you can use the built in java.util.Base64.
    – karl
    Feb 22, 2017 at 8:59
2
import sun.misc.BASE64Decoder;

BASE64Decoder decoder = new BASE64Decoder();
byte[] imageByte = decoder.decodeBuffer(imageData);

Where imageData is String containing Base64 data.

3
  • 1
    Where is that BASE64Decoder from?
    – Thilo
    Jul 13, 2012 at 6:23
  • stackoverflow.com/a/469739/1793718
    – Lucky
    May 29, 2015 at 13:03
  • Your program becomes non-portable because sun.misc package is not part of Java SE API and is not guaranteed to be present in the standard library. Oct 25, 2016 at 12:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.