Back
Close

How to encode/decode in base64 in Java

[CG]BOUGA
2,127 views
// {...}
String string = "some random data";
// Encoding
Encoder encoder = Base64.getEncoder();
byte[] data = string.getBytes(UTF_8);
String encodedString = encoder.encodeToString(data);
System.out.println("Encoded: " + encodedString);
// Decoding
Decoder decoder = Base64.getDecoder();
byte[] bytes = decoder.decode(encodedString);
String decodedString = new String(bytes, UTF_8);
System.out.println("Decoded: " + decodedString);
//{...}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants