Basic Type Base64 Encoding and Decoding in Java Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report Base 64 is an encoding scheme that converts binary data into text format so that encoded textual data can be easily transported over network un-corrupted and without any data loss.(Base 64 format reference).The Basic encoding means no line feeds are added to the output and the output is mapped to a set of characters in A-Za-z0-9+/ character set and the decoder rejects any character outside of this set.Encode simple String into Basic Base 64 format String BasicBase64format= Base64.getEncoder().encodeToString("actualString".getBytes()); Explanation: In above code we called Base64.Encoder using getEncoder() and then get the encoded string by passing the byte value of actualString in encodeToString() method as parameter.Decode Basic Base 64 format to String byte[] actualByte= Base64.getDecoder().decode(encodedString); String actualString= new String(actualByte); Explanation: In above code we called Base64.Decoder using getDecoder() and then decoded the string passed in decode() method as parameter then convert return value to string.Below programs illustrate the Encoding and Decoding in Java:Program 1:Encode simple String into Basic Base 64 format Java // Java program to demonstrate // Encoding simple String into Basic Base 64 format import java.util.*; public class GFG { public static void main(String[] args) { // create a sample String to encode String sample = "India Team will win the Cup"; // print actual String System.out.println("Sample String:\n" + sample); // Encode into Base64 format String BasicBase64format = Base64.getEncoder() .encodeToString(sample.getBytes()); // print encoded String System.out.println("Encoded String:\n" + BasicBase64format); } } Output: Sample String: India Team will win the Cup Encoded String: SW5kaWEgVGVhbSB3aWxsIHdpbiB0aGUgQ3Vw Program 2: Decode Basic Base 64 format to String Java // Java program to demonstrate // Decoding Basic Base 64 format to String import java.util.*; public class GFG { public static void main(String[] args) { // create an encoded String to decode String encoded = "SW5kaWEgVGVhbSB3aWxsIHdpbiB0aGUgQ3Vw"; // print encoded String System.out.println("Encoded String:\n" + encoded); // decode into String from encoded format byte[] actualByte = Base64.getDecoder() .decode(encoded); String actualString = new String(actualByte); // print actual String System.out.println("actual String:\n" + actualString); } } Output: Encoded String: SW5kaWEgVGVhbSB3aWxsIHdpbiB0aGUgQ3Vw actual String: India Team will win the Cup References: https://docs.oracle.com/javase/10/docs/api/java/util/Base64.html#getEncoder() https://docs.oracle.com/javase/9/docs/api/java/util/Base64.html#getDecoder-- https://www.geeksforgeeks.org/dsa/decode-encoded-base-64-string-ascii-string/ https://www.geeksforgeeks.org/dsa/encode-ascii-string-base-64-format/ Comment More infoAdvertise with us Next Article Base conversion in Java A AmanSingh2210 Follow Improve Article Tags : Java Java - util package Java-Functions encoding-decoding Java-Base64 +1 More Practice Tags : Java Similar Reads URL Encoding/Decoding using Base64 in Java Base 64 is an encoding scheme that converts binary data into text format so that encoded textual data can be easily transported over network un-corrupted and without any data loss.(Base 64 format reference). The URL encoding is the same as Basic encoding the only difference is that it encodes or dec 2 min read URL Encoding/Decoding using Base64 in Java Base 64 is an encoding scheme that converts binary data into text format so that encoded textual data can be easily transported over network un-corrupted and without any data loss.(Base 64 format reference). The URL encoding is the same as Basic encoding the only difference is that it encodes or dec 2 min read Base conversion in Java Given a number in a given base, convert it into another target base.Examples Input : Number = "123" Source Base = 8 Target Base = 10 Output : 83 3 * 1 + 2 * 8 + 1 * 64 = 83 Input : Number = "110" Source Base = 2 Target Base = 10 Output : 6 The idea is to use toString() method present in the Integer 3 min read Base conversion in Java Given a number in a given base, convert it into another target base.Examples Input : Number = "123" Source Base = 8 Target Base = 10 Output : 83 3 * 1 + 2 * 8 + 1 * 64 = 83 Input : Number = "110" Source Base = 2 Target Base = 10 Output : 6 The idea is to use toString() method present in the Integer 3 min read CharsetDecoder charset() in Java with examples CharsetDecoder.charset() is an in-built method in Java of CharsetDecoder class that returns the charset that created this decoder. Syntax: public final Charset charset() Parameter: The function does not accepts any parameter. Return value: The function returns the decoder's charset. Program below de 1 min read CharsetEncoder encode(CharBuffer in) method in Java with Examples The encode(CharBuffer input) method is a built-in method of the java.nio.charset.CharsetEncoder which encodes the content which is remaining of a single input character buffer to a newly-allocated byte-buffer. The encode() method in itself implements an entire operation of encoding. This function sh 2 min read Like