Java Decode Base64 String to String

Tags: Java Base64

In this Java tutorial we learn how to decode an encoded Base64 String into a String in Java programming language.

How to decode Base64 String in Java

In Java to decode an encoded Base64 String we can use the Base64.getDecoder().decode() method.

String base64String = "U2ltcGxlIFNvbHV0aW9u";
byte[] decodedByteData = Base64.getDecoder().decode(base64String);
String decodedString = new String(decodedByteData);

The following Java example code to show you how to use the Base64.getDecoder().decode() method to decode a Base64 String in Java application.

Base64ToStringExample1.java

import java.util.Base64;

public class Base64ToStringExample1 {
    public static void main(String... args) {
        String base64String = "U2ltcGxlIFNvbHV0aW9u";

        // Convert Base64 String to Raw String
        byte[] decodedByteData = Base64.getDecoder().decode(base64String);
        String decodedString = new String(decodedByteData);

        System.out.println("Base64 String:");
        System.out.println(base64String);
        System.out.println("Decoded String:");
        System.out.println(decodedString);
    }
}
The output as below.
Base64 String:
U2ltcGxlIFNvbHV0aW9u
Decoded String:
Simple Solution

Happy Coding 😊

Java Encode String to Base64 String