Learn how to Base64 Encode a String in Java

0
5
Adv1


Adv2

Fast resolution

In brief, you possibly can simply do that:

new String(Base64.getEncoder().encode(bytes));

Examples and an evidence

In Java 8 and above, you simply have to import the Base64 class:

import java.util.Base64;

Then use the Base64 static strategies as follows:

byte[] encodedBytes = Base64.getEncoder().encode("Check".getBytes());
System.out.println("encodedBytes " + new String(encodedBytes));

byte[] decodedBytes = Base64.getDecoder().decode(encodedBytes);
System.out.println("decodedBytes " + new String(decodedBytes));

If you wish to straight encode a string and get the consequence as an encoded string:

String encodeBytes = Base64.getEncoder()
    .encodeToString(("Your String").getBytes());
Adv3