Base64 Encoder/Decoder — Developer Code Samples

Base64 encoding converts binary data to ASCII text using a 64-character alphabet. Use Python's base64 module, JavaScript's btoa/atob functions, or the base64 CLI tool for fast encoding and decoding without any external libraries.

Try the interactive version online: Open Base64 Encode/Decode Tool →

Parameters

Parameter Type Required Description
text str Yes Input text or binary data to encode
url_safe bool No Use URL-safe alphabet (- and _ instead of + and /)
encoding str No Character encoding for text input (default: utf-8)

Returns: Base64-encoded ASCII string, or decoded original text/bytes

Code Examples

import base64

# Encode text to Base64
text = "Hello, World!"
encoded = base64.b64encode(text.encode('utf-8')).decode('ascii')
print(encoded)
# Output: SGVsbG8sIFdvcmxkIQ==

# Decode Base64 back to text
decoded = base64.b64decode(encoded).decode('utf-8')
print(decoded)
# Output: Hello, World!

# URL-safe Base64 (no + or / characters)
url_safe = base64.urlsafe_b64encode(b"binary data here").decode('ascii')
print(url_safe)

# Encode a file to Base64
def encode_file(filepath):
    with open(filepath, 'rb') as f:
        return base64.b64encode(f.read()).decode('ascii')

# Decode Base64 and write to file
def decode_to_file(b64_string, output_path):
    data = base64.b64decode(b64_string)
    with open(output_path, 'wb') as f:
        f.write(data)