DEV Community

arenasbob2024-cell
arenasbob2024-cell

Posted on • Originally published at viadreams.cc

Base64 Encoding: Everything Developers Need to Know

Base64 encoding is everywhere - data URLs, email attachments, JWT tokens, API payloads. Here's what every developer should know.

What is Base64?

Base64 converts binary data into ASCII text using 64 characters (A-Z, a-z, 0-9, +, /). It increases size by ~33% but ensures safe transmission through text-only channels.

Common Use Cases

Use Case Example
Data URLs data:image/png;base64,iVBOR...
Email (MIME) Attachments encoded in Base64
JWT tokens Header.Payload.Signature
API auth Authorization: Basic base64(user:pass)
Embedding images <img src="data:image/png;base64,...">

JavaScript

// Encode
btoa('Hello World')  // 'SGVsbG8gV29ybGQ='

// Decode
atob('SGVsbG8gV29ybGQ=')  // 'Hello World'

// For Unicode (UTF-8)
const encode = (str) => btoa(unescape(encodeURIComponent(str)));
const decode = (str) => decodeURIComponent(escape(atob(str)));
Enter fullscreen mode Exit fullscreen mode

Python

import base64

# Encode
base64.b64encode(b'Hello World').decode()  # 'SGVsbG8gV29ybGQ='

# Decode
base64.b64decode('SGVsbG8gV29ybGQ=').decode()  # 'Hello World'

# URL-safe variant
base64.urlsafe_b64encode(data)
Enter fullscreen mode Exit fullscreen mode

Command Line

# Encode
echo -n 'Hello World' | base64
# SGVsbG8gV29ybGQ=

# Decode
echo 'SGVsbG8gV29ybGQ=' | base64 -d
# Hello World
Enter fullscreen mode Exit fullscreen mode

Try It Online

Encode and decode Base64 with DevToolBox - handles text, files, images, and URL-safe variants.


What's your most creative use of Base64? Share below!

Top comments (0)