Java
Base64
Byte Array
Data Conversion
Programming Tips

How to convert a byte array to a Base64 string in Java

Codemia Knowledge Hub ·

In Java 8 and later, use java.util.Base64: Base64.getEncoder().encodeToString(byteArray) returns the Base64 string. For URLs and filenames, use Base64.getUrlEncoder() instead.

Base64 turns binary data into a string of ASCII characters so it can travel through media built for text, such as JSON, XML, web APIs or images embedded in HTML. Three properties make it useful:

  1. Compatibility: binary data becomes text that text-based protocols carry without trouble.
  2. Data integrity: it is an encoding, not encryption, so the data stays unchanged in transport and decodes back to the same bytes.
  3. Universal support: many systems understand Base64, so the data stays readable across platforms.

How Base64 encodes data

Base64 represents binary data in radix 64. Its alphabet has 64 characters: uppercase and lowercase letters (A to Z, a to z), the digits 0 to 9, and the symbols + and /. The = character is padding that makes the encoded length a multiple of 4.

Basic encoding with Base64.getEncoder()

java.util.Base64 offers two main encoders: the basic encoder, for simple conversions that need no line breaks or headers, and the URL encoder, which uses the URL and filename safe alphabet. With the basic encoder:

  1. Import the class: java.util.Base64.
  2. Get an encoder: obtain a Base64.Encoder instance.
  3. Encode: convert the byte array to a Base64 string.
java
1import java.util.Base64;
2
3public class Base64Example {
4
5    public static void main(String[] args) {
6        // Example byte array
7        byte[] byteArray = {1, 2, 3, 4, 5};
8
9        // Get encoder
10        Base64.Encoder encoder = Base64.getEncoder();
11
12        // Encode the byte array
13        String base64Encoded = encoder.encodeToString(byteArray);
14
15        // Display the result
16        System.out.println("Base64 Encoded String: " + base64Encoded);
17    }
18}
  • Base64.getEncoder() returns the Base64.Encoder.
  • Its encodeToString(byte[] src) method encodes the array.
  • The result is a regular String that you can display, store or send.

URL and filename safe encoding

For URLs, filenames and some network protocols, the URL-safe variant replaces + with - and / with _:

java
1import java.util.Base64;
2
3public class Base64URLExample {
4
5    public static void main(String[] args) {
6        // Example byte array
7        byte[] byteArray = {1, 2, 3, 4, 5};
8
9        // Get URL encoder
10        Base64.Encoder urlEncoder = Base64.getUrlEncoder();
11
12        // Encode the byte array
13        String urlSafeBase64 = urlEncoder.encodeToString(byteArray);
14
15        // Display the result
16        System.out.println("URL Safe Base64 Encoded String: " + urlSafeBase64);
17    }
18}

Quick reference

ComponentDescription
Base64.getEncoder()Returns a basic Base64 encoder.
encodeToString()Encodes a byte array to a Base64 string.
URL safe encodingUse Base64.getUrlEncoder().

Padding, performance and decoding

  • Padding: when the array length is not a multiple of 3, the output is padded with = so its length is a multiple of 4.
  • Performance: encoding and decoding large arrays can be expensive. Profile and manage resources if you do it often with large data.
  • Decoding: Base64.Decoder reverses the process, for example with decode(String src).

Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms