Java 8+

Encode or decode byte arrays:

byte[] encoded = Base64.getEncoder().encode("Hello".getBytes());
println(new String(encoded));   // Outputs "SGVsbG8="

byte[] decoded = Base64.getDecoder().decode(encoded);
println(new String(decoded))    // Outputs "Hello"

Or if you just want the strings:

String encoded = Base64.getEncoder().encodeToString("Hello".getBytes());
println(encoded);   // Outputs "SGVsbG8="

String decoded = new String(Base64.getDecoder().decode(encoded.getBytes()));
println(decoded)    // Outputs "Hello"

For more info, see Base64.

Java < 8

Base64 is not bundled with Java versions less than 8. I recommend using Apache Commons Codec.

For direct byte arrays:

Base64 codec = new Base64();
byte[] encoded = codec.encode("Hello".getBytes());
println(new String(encoded));   // Outputs "SGVsbG8="

byte[] decoded = codec.decode(encoded);
println(new String(decoded))    // Outputs "Hello"

Or if you just want the strings:

Base64 codec = new Base64();
String encoded = codec.encodeBase64String("Hello".getBytes());
println(encoded);   // Outputs "SGVsbG8="

String decoded = new String(codec.decodeBase64(encoded));
println(decoded)    // Outputs "Hello"

Android (with Java < 8)

If you are using the Android SDK before Java 8 then your best option is to use the bundled android.util.Base64.

For direct byte arrays:

byte[] encoded = Base64.encode("Hello".getBytes());
println(new String(encoded))    // Outputs "SGVsbG8="

byte [] decoded = Base64.decode(encoded);
println(new String(decoded))    // Outputs "Hello"

Or if you just want the strings:

String encoded = Base64.encodeToString("Hello".getBytes());
println(encoded);   // Outputs "SGVsbG8="

String decoded = new String(Base64.decode(encoded));
println(decoded)    // Outputs "Hello"

Other: Spring < 6.0.5

If for some reason Java's Base64 isn't suitable and you use Spring, see Base64Utils. This package, however, is deprecated as of 6.0.5 and so I don't recommend.

Answer from Cory Klein on Stack Overflow
Top answer
1 of 4
395

Java 8+

Encode or decode byte arrays:

byte[] encoded = Base64.getEncoder().encode("Hello".getBytes());
println(new String(encoded));   // Outputs "SGVsbG8="

byte[] decoded = Base64.getDecoder().decode(encoded);
println(new String(decoded))    // Outputs "Hello"

Or if you just want the strings:

String encoded = Base64.getEncoder().encodeToString("Hello".getBytes());
println(encoded);   // Outputs "SGVsbG8="

String decoded = new String(Base64.getDecoder().decode(encoded.getBytes()));
println(decoded)    // Outputs "Hello"

For more info, see Base64.

Java < 8

Base64 is not bundled with Java versions less than 8. I recommend using Apache Commons Codec.

For direct byte arrays:

Base64 codec = new Base64();
byte[] encoded = codec.encode("Hello".getBytes());
println(new String(encoded));   // Outputs "SGVsbG8="

byte[] decoded = codec.decode(encoded);
println(new String(decoded))    // Outputs "Hello"

Or if you just want the strings:

Base64 codec = new Base64();
String encoded = codec.encodeBase64String("Hello".getBytes());
println(encoded);   // Outputs "SGVsbG8="

String decoded = new String(codec.decodeBase64(encoded));
println(decoded)    // Outputs "Hello"

Android (with Java < 8)

If you are using the Android SDK before Java 8 then your best option is to use the bundled android.util.Base64.

For direct byte arrays:

byte[] encoded = Base64.encode("Hello".getBytes());
println(new String(encoded))    // Outputs "SGVsbG8="

byte [] decoded = Base64.decode(encoded);
println(new String(decoded))    // Outputs "Hello"

Or if you just want the strings:

String encoded = Base64.encodeToString("Hello".getBytes());
println(encoded);   // Outputs "SGVsbG8="

String decoded = new String(Base64.decode(encoded));
println(decoded)    // Outputs "Hello"

Other: Spring < 6.0.5

If for some reason Java's Base64 isn't suitable and you use Spring, see Base64Utils. This package, however, is deprecated as of 6.0.5 and so I don't recommend.

2 of 4
35

Use:

byte[] data = Base64.encode(base64str);

Encoding converts to Base64

You would need to reference commons codec from your project in order for that code to work.

For java8:

import java.util.Base64
🌐
Java2Blog
java2blog.com › home › core java › convert byte array to base64 string in java
Convert Byte Array to Base64 String in Java - Java2Blog
February 12, 2022 - Java 8 has finally introduced Base64 functionalities via java.util.Base64 class. You can use Base64's encode() method to convert Byte Array to Base64 String in Java. It encodes input without any line separation.
Discussions

java - How to convert byte array to base64 String - Stack Overflow
hello kevin , i dont get any exception ... gets converted to base64 , i need to all images in the list . ... Save this answer. ... Show activity on this post. ... The image bytes you have do not represent UTF-8 encoded text, so it makes no sense to create a String out of the bytes, pretending that this is UTF-8 encoded text. If you are using Java 8, then use ... More on stackoverflow.com
🌐 stackoverflow.com
Converting byte array to base64 string java - Stack Overflow
Also, are you using the encoded base64 data in an URL. Use the URLSafe version only when the base64 encoding goes into a http/get or some other url-related operation. ... @developer : All I use to convert the byte[] to string is a single methid which i have already provided in my question... More on stackoverflow.com
🌐 stackoverflow.com
April 25, 2017
java - Convert byte[] to Base64 string for data URI - Stack Overflow
I know this has probably been asked 10000 times, however, I can't seem to find a straight answer to the question. I have a LOB stored in my db that represents an image; I am getting that image fr... More on stackoverflow.com
🌐 stackoverflow.com
java - convert byte array to Base64 String in android - Stack Overflow
So I have the user enter password to signup from the android app. Before I save the password to the database on the server I want to convert it to a MD5 one way hash and save it to the database. More on stackoverflow.com
🌐 stackoverflow.com
🌐
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.
🌐
Simplesolution
simplesolution.dev › java-convert-byte-array-to-base64-string
Java Convert Byte Array to Base64 String
In this Java tutorial we learn ... language. In Java to convert a byte[] array to Base64 String we can use the Base64.getEncoder().encodeToString() method....
🌐
Blogger
javahungry.blogspot.com › 2022 › 10 › byte-array-to-base64-string.html
Convert byte array to Base64 String in Java with examples | Java Hungry
import android.util.Base64; public class ByteArrayToBase64String4 { public static void main(String args[]) { // using android Base64 class encode() method byte[] encoded = Base64.getEncoder().encode("AliveisAwesome".getBytes()); String output = new String(encoded); System.out.println(output); // One liner using encodeToString() String encodedString = Base64.getEncoder().encodeToString("AliveisAwesome".getBytes()); System.out.println(encodedString); } } Output: QWxpdmVpc0F3ZXNvbWU= QWxpdmVpc0F3ZXNvbWU= That's all for today, please mention in the comments if you have any questions related to how to convert byte array to Base64 String in Java with examples.
🌐
Codemia
codemia.io › home › knowledge hub › how do i convert a byte array to base64 in java?
How do I convert a byte array to Base64 in Java? | Codemia
July 30, 2025 - Import the Base64 Class: Import the necessary Base64 class from java.util. Get a Base64 Encoder: Create an instance of the Base64.Encoder. Encode the Byte Array: Use the encoder to convert the byte array into a Base64 string.
Find elsewhere
🌐
Apps Developer Blog
appsdeveloperblog.com › home › java › convert image byte[] array to base64 encoded string in java
Convert Image byte[] Array to Base64 encoded String in Java - Apps Developer Blog
July 21, 2022 - Convert image to an array of bytes byte[] imageBytes = Files.readAllBytes(pathToImage); // 2. Encode image bytes[] to Base64 encoded String String base64EncodedImageBytes = Base64.getEncoder().encodeToString(imageBytes); System.out.println(base64EncodedImageBytes); // 3. Decode Base64 encoded ...
🌐
CodingTechRoom
codingtechroom.com › question › convert-byte-array-to-base64-java
How to Convert a Byte Array to Base64 in Java - CodingTechRoom
Import the `java.util.Base64` library. Use `Base64.getEncoder().encodeToString(byte[])` to convert to Base64. Use `Base64.getDecoder().decode(String)` to convert back to byte array.
🌐
javaspring
javaspring.net › blog › how-do-i-convert-a-byte-array-to-base64-in-java
How to Convert Byte Array to Base64 in Java: Step-by-Step Guide (C# Equivalent) — javaspring.net
Converting a byte array to Base64 is straightforward in both Java and C# using built-in utilities. Java’s java.util.Base64.Encoder and C#’s Convert.ToBase64String handle the heavy lifting, with stream-based options for large data.
🌐
MojoAuth
mojoauth.com › binary-encoding-decoding › base64-with-java
Base64 with Java | Binary Encoding Techniques Across Programming Languages
Java's built-in java.util.Base64 class provides a straightforward way to encode data. The primary method you'll use is getEncoder().encodeToString(byte[]), which takes a byte array as input and returns the Base64 encoded string.
🌐
Medium
medium.com › @yeung.ming.sang › use-multi-thread-for-base64-encoding-in-java-15218e409f7e
Use multi-thread for Base64 encoding in Java | by Andy | Medium
November 1, 2023 - Typically, we use the following way to encode the byte[] to base64 string. import java.util.Base64; private static String convertToBase64(byte[] bytes) { return Base64.getEncoder().encodeToString(bytes); }
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Base64.Encoder.html
Base64.Encoder (Java Platform SE 8 )
July 21, 2026 - Encodes all bytes from the specified byte array using the Base64 encoding scheme, writing the resulting bytes to the given output byte array, starting at offset 0.
🌐
javathinking
javathinking.com › blog › convert-byte-array-to-base-64-string-java
Convert Byte Array to Base 64 String in Java — javathinking.com
A: Java's java.util.Base64 class is optimized for performance and can handle large byte arrays efficiently.
🌐
Stack Abuse
stackabuse.com › encoding-and-decoding-base64-strings-in-java
Encoding and Decoding Base64 Strings in Java
September 7, 2023 - Though, instead of being used as ... as isBase64(), isInAlphabet() and isUrlSafe(). Let's go ahead and encode a simple string into Base64. The encodeBase64() method accepts a byte array, and returns a byte array, representing the encoded input....
🌐
CodeProject
codeproject.com › Questions › 145581 › Convert-byte-array-to-base64-string
Convert byte array to base64 string
Aviso legal: las referencias a una empresa, producto o servicios específicos en este Sitio no están controladas por GoDaddy.com LLC y no constituyen ni implican la asociación ni respaldo a anunciantes externos