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 Overflow
🌐
Baeldung
baeldung.com › home › java › java base64 encoding and decoding
Java Base64 Encoding and Decoding | Baeldung
February 18, 2026 - String originalInput = "test input"; String encodedString = Base64.getEncoder().encodeToString(originalInput.getBytes()) + "\n"; IllegalArgumentException exception = assertThrows( IllegalArgumentException.class, () -> Base64.getDecoder().decode(encodedString) ); assertTrue(exception.getMessage().startsWith("Input byte array has incorrect ending byte at")); As we can see, even though the Base64 content itself is valid, the trailing newline causes the decoder to reject the input. This is because Java’s Base64.getDecoder() is strict.
Discussions

Help needed to decode base64 string to byte array.
Note that data.codec is now not necessary and you can just use https://docs.oracle.com/javase/8/docs/api/java/util/Base64.html in the JDK More on reddit.com
🌐 r/Clojure
7
4
June 4, 2024
convert base64 String to Byte code in java - Stack Overflow
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 ... More on stackoverflow.com
🌐 stackoverflow.com
Decode base64 data in Java - Stack Overflow
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). More on stackoverflow.com
🌐 stackoverflow.com
Byte[] to string and vice versa not working
What exactly are you trying to do? Serialize a byte array into a string in a way that can recover it? Use Convert.ToBase64String It looks like you're trying to save an image represented by a byte array as a string, but you're trying to convert it to some text which can easily lose information. That's why the byte array you get back doesn't work - it's not the same byte array. Base64 string will fix your problem More on reddit.com
🌐 r/csharp
28
14
December 18, 2022
🌐
Medium
medium.com › @rkgupta1298 › base64-encoding-in-java-7ff71ea0cc65
Base64 Encoding and Decoding in JAVA | by coffeebytes | Medium
March 15, 2022 - To decode any string, we can use the decode() method of Decoder class which is an inner class of Base64. The decode() method returns a byte array instead of string, that can be converted into a string using the String constructor.
🌐
Reddit
reddit.com › r/clojure › help needed to decode base64 string to byte array.
r/Clojure on Reddit: Help needed to decode base64 string to byte array.
June 4, 2024 -

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
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Base64.Decoder.html
Base64.Decoder (Java Platform SE 8 )
July 21, 2026 - Decodes all bytes from the input byte array using the Base64 encoding scheme, writing the results into the given output byte array, starting at offset 0.
🌐
InfoWorld
infoworld.com › home › software development › programming languages › java
Base64 encoding and decoding in Java 8 | InfoWorld
December 6, 2017 - Passing the null reference to one of the following methods results in NullPointerException: byte[] decode(byte[] src): Decode all bytes in src to a newly-allocated byte array, which this method returns.
Find elsewhere
🌐
Stack Abuse
stackabuse.com › encoding-and-decoding-base64-strings-in-java
Encoding and Decoding Base64 Strings in Java
September 7, 2023 - Encoder encoder = Base64.getUr... The encode() method would encode the input into a byte array. Again, the decode() method works with a byte array and decodes the Base64 string into the original one:...
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › util › Base64.Decoder.html
Base64.Decoder (Java SE 11 & JDK 11 )
January 20, 2026 - Decodes all bytes from the input byte array using the Base64 encoding scheme, writing the results into the given output byte array, starting at offset 0.
🌐
Techie Delight
techiedelight.com › home › java › base64 encoding and decoding in java
Base64 Encoding and Decoding in Java | Techie Delight
July 7, 2026 - The following program uses Base64.Encoder.encodeToString() method for encoding the specified byte array into a Base64 encoded string and Base64.Decoder.decode() method for decoding back the Base64 encoded string into a newly allocated byte array.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.util.base64.decoder.decode
Base64.Decoder.Decode Method (Java.Util) | Microsoft Learn
Decodes all bytes from the input byte array using the Base64 encoding scheme, writing the results into the given output byte array, starting at offset 0.
🌐
Oracle
docs.oracle.com › en › java › javase › 21 › docs › api › java.base › java › util › Base64.Decoder.html
Base64.Decoder (Java SE 21 & JDK 21)
January 20, 2026 - Returns an input stream for decoding Base64 encoded byte stream. clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait ... Decodes all bytes from the input byte array using the Base64 encoding scheme, writing the results into a newly-allocated output byte array.
🌐
MojoAuth
mojoauth.com › binary-encoding-decoding › base64-with-java
Base64 with Java | Binary Encoding Techniques Across Programming Languages
Java's java.util.Base64 class also handles decoding Base64 encoded strings. You'll use the getDecoder().decode(String) method, which takes your encoded string and returns a byte array representing the original data.
🌐
Oracle
docs.oracle.com › en › java › javase › 16 › docs › api › java.base › java › util › Base64.Decoder.html
Base64.Decoder (Java SE 16 & JDK 16)
January 6, 2022 - Returns an input stream for decoding Base64 encoded byte stream. clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait ... Decodes all bytes from the input byte array using the Base64 encoding scheme, writing the results into a newly-allocated output byte array.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Base64.Encoder.html
Base64.Encoder (Java Platform SE 8 )
July 21, 2026 - Instances of Base64.Encoder class ... class will cause a NullPointerException to be thrown. ... Encodes all bytes from the specified byte array into a newly-allocated byte array using the Base64 encoding scheme....
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.base › java › util › Base64.Decoder.html
Base64.Decoder (Java SE 17 & JDK 17)
April 21, 2026 - Returns an input stream for decoding Base64 encoded byte stream. clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait ... Decodes all bytes from the input byte array using the Base64 encoding scheme, writing the results into a newly-allocated output byte array.
🌐
DEV Community
dev.to › tooldeck › base64-decode-in-java-getdecoder-geturldecoder-getmimedecoder-and-streaming-3b9o
Base64 Decode in Java: getDecoder, getUrlDecoder, getMimeDecoder, and Streaming - DEV Community
April 9, 2026 - import java.util.Base64; import java.nio.ByteBuffer; import java.util.UUID; public class DecodeUUID { public static UUID fromBase64(String encoded) { byte[] bytes = Base64.getUrlDecoder().decode(encoded); ByteBuffer bb = ByteBuffer.wrap(bytes); return new UUID(bb.getLong(), bb.getLong()); } public static void main(String[] args) { // Compact Base64-encoded UUID from an API response String uuidStr = "f47ac10b-58cc-4372-a567-0e02b2c3d479"; UUID original = UUID.fromString(uuidStr); // Encode to Base64 (compact form — 22 chars vs 36) ByteBuffer bb = ByteBuffer.wrap(new byte[16]); bb.putLong(orig