You can use java.util.Base64 package to decode the String to byte[].
Below code which I have used for encode and decode.
For Java 8 :
import java.io.UnsupportedEncodingException;
import java.util.Base64;
public class Example {
public static void main(String[] args) {
try {
byte[] name = Base64.getEncoder().encode("hello World".getBytes());
byte[] decodedString = Base64.getDecoder().decode(new String(name).getBytes("UTF-8"));
System.out.println(new String(decodedString));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
For Java 6 :
import java.io.UnsupportedEncodingException;
import org.apache.commons.codec.binary.Base64;
public class Main {
public static void main(String[] args) {
try {
byte[] name = Base64.encodeBase64("hello World".getBytes());
byte[] decodedString = Base64.decodeBase64(new String(name).getBytes("UTF-8"));
System.out.println(new String(decodedString));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Answer from Ravi Koradia on Stack OverflowHelp needed to decode base64 string to byte array.
convert base64 String to Byte code in java - Stack Overflow
Decode base64 data in Java - Stack Overflow
Byte[] to string and vice versa not working
I am having a hard time trying to decode base64 string to byte array.
I have base64 encoded string of an image and I want to decode to byte array.
This is what I tried.
imported clojure.data.codec.base64 :as b64
``` (def base64-img "iVBORw0KGgoAAAANSUhEUgAAAAgAAAAIAQMAAAD+wSzIAAAABlBMVEX///+/v7+jQ3Y5AAAADklEQVQI12P4AIX8EAgALgAD/aNpbtEAAAAASUVORK5CYII") (defn base64->bytes [base64-str] (b64/decode (.getBytes base64-str "UTF-8"))) (def b64-bytes (base64->bytes base64-img)) ;; output ;; b64-bytes => nil
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());
import sun.misc.BASE64Decoder;
BASE64Decoder decoder = new BASE64Decoder();
byte[] imageByte = decoder.decodeBuffer(imageData);
Where imageData is String containing Base64 data.
As of Java 8, there is an officially supported API for Base64 encoding and decoding. In time this will probably become the default choice.
The API includes the class java.util.Base64 and its nested classes. It supports three different flavors: basic, URL safe, and MIME.
Sample code using the "basic" encoding:
import java.util.Base64;
byte[] bytes = "Hello, World!".getBytes("UTF-8");
String encoded = Base64.getEncoder().encodeToString(bytes);
byte[] decoded = Base64.getDecoder().decode(encoded);
String decodedStr = new String(decoded, StandardCharsets.UTF_8);
The documentation for java.util.Base64 includes several more methods for configuring encoders and decoders, and for using different classes as inputs and outputs (byte arrays, strings, ByteBuffers, java.io streams).
As of v6, Java SE ships with JAXB. javax.xml.bind.DatatypeConverter has static methods that make this easy. See parseBase64Binary() and printBase64Binary().
UPDATE: JAXB is no longer shipped with Java (since Java 11). If JAXB is required for your project, you will need to configure the relevant libraries via your dependency management system, for example Maven. If you require the compiler (xjc.exe) you also need to download that separately.