Utilities: Base64 encodes bytes into ASCII text and decodes them back

Base64.getEncoder produces an ASCII-safe string from arbitrary bytes, and Base64.getDecoder reverses it exactly, which is how binary data travels safely through text-only channels like JSON or URLs.

Code
byte[] original = "hello, base64".getBytes(java.nio.charset.StandardCharsets.UTF_8);
String encoded = Base64.getEncoder().encodeToString(original);
System.out.println("Encoded: " + encoded);
byte[] decoded = Base64.getDecoder().decode(encoded);
System.out.println("Decoded: " + new String(decoded, java.nio.charset.StandardCharsets.UTF_8));
Output
Encoded: aGVsbG8sIGJhc2U2NA==
Decoded: hello, base64
Advertisement
More in JAVA

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-09-27

© Java Coding Hub · About · Contact · Privacy · Terms