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:
- Compatibility: binary data becomes text that text-based protocols carry without trouble.
- Data integrity: it is an encoding, not encryption, so the data stays unchanged in transport and decodes back to the same bytes.
- 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:
- Import the class:
java.util.Base64. - Get an encoder: obtain a
Base64.Encoderinstance. - Encode: convert the byte array to a Base64 string.
Base64.getEncoder()returns theBase64.Encoder.- Its
encodeToString(byte[] src)method encodes the array. - The result is a regular
Stringthat 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 _:
Quick reference
| Component | Description |
Base64.getEncoder() | Returns a basic Base64 encoder. |
encodeToString() | Encodes a byte array to a Base64 string. |
| URL safe encoding | Use 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.Decoderreverses the process, for example withdecode(String src).
Related reading
- How do I convert a Java 8 IntStream to a List?
- How do I convert a Map to List in Java?
- How do I convert a numpy array to and display an image?
- How do I convert a Swift Array to a String?
- How do I convert a String to an InputStream in Java?
- How do I convert an OutputStream to an InputStream?
- How do I convert all of the items in a list to floats?
- How do I convert all strings in a list of lists to integers?
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

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 courseData 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.