Sobes.tech
Junior — Middle

Can you explain what Base64 encoding is and what it is used for?

sobes.tech AI

Answer from AI

Base64 is a method of encoding binary data into a text format using a set of 64 characters (uppercase and lowercase Latin letters, digits, and two additional symbols).

The main purpose of Base64 is to securely transmit binary data (such as images, files) over text-based protocols that do not support binary data directly, such as email or JSON.

Example of usage in Java:

import java.util.Base64;

public class Base64Example {
    public static void main(String[] args) {
        String original = "Sample text";
        String encoded = Base64.getEncoder().encodeToString(original.getBytes());
        System.out.println("Encoded: " + encoded);

        byte[] decodedBytes = Base64.getDecoder().decode(encoded);
        String decoded = new String(decodedBytes);
        System.out.println("Decoded: " + decoded);
    }
}

Thus, Base64 helps to securely transmit and store data in text form.