Base64 Encoder/Decoder
Encode text to Base64 format and decode Base64 strings to plain text
This tool allows you to convert text to Base64 format and decode Base64 strings back to plain text.
Base64 Encoder/Decoder
Tips: Base64 encoding increases size by approximately 33%. For UTF-8 text with non-ASCII characters, the browser's built-in encoder/decoder is used for proper handling.
About Base64 Encoding
Base64 is a binary-to-text encoding scheme that represents binary data in an ASCII string format. It's commonly used when binary data needs to be transmitted over media designed to handle text.
Key aspects of Base64 encoding:
- Character Set: Base64 uses a set of 64 characters (A-Z, a-z, 0-9, + and /) plus a padding character (=).
- Padding: The equals sign (=) is used as padding at the end of encoded data to make the length a multiple of 4.
- Expansion: Base64 encoding increases the size of data by approximately 33% (4 characters for every 3 bytes).
- Universally Supported: Almost all programming languages have built-in functions to encode and decode Base64.
This tool performs all encoding and decoding in your browser, ensuring your data remains private and secure.
Base64 Examples and Use Cases
Here's a simple example of Base64 encoding:
Plain Text | Base64 Encoded |
---|---|
Hello, World! | SGVsbG8sIFdvcmxkIQ== |
Hello | SGVsbG8= |
Base64 | QmFzZTY0 |
Common Use Cases for Base64 Encoding
Use Case | Description |
---|---|
Email Attachments | MIME format uses Base64 to encode binary attachments in emails |
Data URIs | Embedding images and other resources directly in HTML/CSS using Base64 encoding |
Basic Authentication | HTTP Basic Authentication encodes username:password pairs in Base64 |
JSON Web Tokens (JWT) | JWTs use Base64 encoding for transmitting data between parties |
XML/Binary Data | XML sometimes uses Base64 to include binary content |
Base64 in Different Programming Languages
Here are examples of how to encode and decode Base64 in different programming languages:
JavaScript:
// Encoding to Base64
const text = "Hello, World!";
const encoded = btoa(text); // "SGVsbG8sIFdvcmxkIQ=="
// Decoding from Base64
const decoded = atob("SGVsbG8sIFdvcmxkIQ=="); // "Hello, World!"
// For UTF-8 support
const utf8Encode = new TextEncoder();
const utf8Decode = new TextDecoder();
// Encode UTF-8 text to Base64
const utf8Text = "Hello, World!";
const bytes = utf8Encode.encode(utf8Text);
const base64 = btoa(String.fromCharCode.apply(null, bytes));
// Decode Base64 to UTF-8 text
const binaryString = atob(base64);
const bytesDecoded = new Uint8Array(binaryString.length);
for (let i = 0; i < binaryString.length; i++) {
bytesDecoded[i] = binaryString.charCodeAt(i);
}
const decodedText = utf8Decode.decode(bytesDecoded); // "Hello, World!"
Python:
import base64
# Encoding to Base64
text = "Hello, World!"
encoded = base64.b64encode(text.encode('utf-8'))
print(encoded.decode('utf-8')) # "SGVsbG8sIFdvcmxkIQ=="
# Decoding from Base64
decoded = base64.b64decode("SGVsbG8sIFdvcmxkIQ==").decode('utf-8')
print(decoded) # "Hello, World!"
Java:
import java.util.Base64;
// Encoding to Base64
String text = "Hello, World!";
String encoded = Base64.getEncoder().encodeToString(text.getBytes());
System.out.println(encoded); // "SGVsbG8sIFdvcmxkIQ=="
// Decoding from Base64
byte[] decodedBytes = Base64.getDecoder().decode("SGVsbG8sIFdvcmxkIQ==");
String decoded = new String(decodedBytes);
System.out.println(decoded); // "Hello, World!"