Junior — Middle
What is the main purpose of encoding data in Base64 format?
sobes.tech AI
Answer from AI
The main task of encoding data in Base64 format is to convert binary data into a text format that can be safely transmitted over text-based protocols such as HTTP, email, or XML. Base64 encodes data using a set of 64 ASCII characters, which helps avoid issues with incompatible characters and preserves data integrity during transmission.
Example 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);
}
}