Binary-to-text conversion demystified for web developers
You have probably seen Base64 strings before — those long blocks of letters, numbers, and occasional +, /, and = characters. They appear in JWT tokens, email headers, data URIs, and API requests. But what is Base64 actually doing, and when should you use it? This guide explains how Base64 encoding works from the ground up, covers every common use case, and warns you about the situations where it causes more problems than it solves.
Base64 is a binary-to-text encoding scheme that converts arbitrary binary data into a string of ASCII characters. It was designed to solve a specific problem: how do you safely send binary data (images, files, raw bytes) through systems that only handle text?
Email was the original use case. SMTP (the email protocol) was designed for 7-bit ASCII text. If you tried to send an image or a PDF attachment as raw bytes, the email servers would corrupt it — some bytes would be interpreted as control characters, others would be stripped. Base64 encodes those bytes into safe, printable characters that survive any text-based transport without corruption.
The name "Base64" comes from the encoding alphabet: it uses 64 distinct characters to represent data. These are A-Z (26), a-z (26), 0-9 (10), + (1), and / (1). The = character is used for padding at the end when the input length is not a multiple of 3.
Let us walk through encoding the string Hi! to Base64.
Step 1: Convert characters to ASCII byte values.
H = 72, i = 105, ! = 33Step 2: Convert each byte to 8-bit binary.
010010000110100100100001Step 3: Concatenate all bits into one continuous string.
010010000110100100100001 (24 bits total)
Step 4: Split into 6-bit groups.
010010 = 18000110 = 6100100 = 36100001 = 33Step 5: Map each 6-bit value to the Base64 alphabet.
S, 6 = G, 36 = k, 33 = hResult: Hi! encodes to SGkh
When the input length is not a multiple of 3 bytes, Base64 pads the output with = characters. For example, Hi (2 bytes) encodes to SGk=, and H (1 byte) encodes to SA==. The padding tells the decoder how many bytes were in the original input.
Standard Base64 uses + and / in its alphabet. These characters have special meaning in URLs: + is interpreted as a space, and / is a path separator. If you put standard Base64 in a URL, it breaks.
URL-safe Base64 (also called Base64url, defined in RFC 4648) solves this by replacing two characters:
+ becomes - (hyphen)/ becomes _ (underscore)= is typically omitted (since the decoder can infer the padding from the string length)This is the encoding used in JWT tokens, which frequently appear in URLs and HTTP headers. If you are seeing - and _ in a Base64-like string, it is URL-safe Base64. Our Base64 tool supports both standard and URL-safe variants.
You can embed small images directly in HTML or CSS using data URIs:
<img src="data:image/png;base64,iVBORw0KGgo..." />
This eliminates an HTTP request for the image — the browser decodes the Base64 string and renders the image directly. This is useful for small icons and UI elements (under 10 KB) where the overhead of an additional HTTP request outweighs the 33% size increase from Base64 encoding.
When you send an email with an attachment, the email client Base64-encodes the file and embeds it in the email body using MIME (Multipurpose Internet Mail Extensions). The receiving email client decodes it back to the original file. This is how email has handled attachments since the early 1990s.
JSON Web Tokens encode their header and payload as URL-safe Base64 strings. A JWT like eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.signature is just two Base64url-encoded JSON objects separated by dots, followed by a signature. You can decode the header and payload with any Base64 decoder to inspect the token contents.
HTTP Basic authentication encodes credentials as username:password in Base64. The Authorization header looks like Basic dXNlcjpwYXNz, where dXNlcjpwYXNz is the Base64 encoding of user:pass. This is encoding, not encryption — anyone who intercepts the header can decode the credentials instantly. Always use HTTPS with Basic auth.
JSON has no binary data type. If you need to include binary data in a JSON payload — an image thumbnail, a file attachment, a cryptographic signature — you Base64-encode it into a string. The receiving system decodes the string back to binary. This is common in REST APIs that handle file uploads via JSON request bodies.
Every 3 bytes of input become 4 bytes of Base64 output. That is a 33% increase in size. This overhead matters in several scenarios:
When NOT to use Base64:
In browsers, use btoa() to encode and atob() to decode. These functions handle ASCII strings only. For Unicode strings, first encode to UTF-8 bytes:
btoa('Hello') returns SGVsbG8=
atob('SGVsbG8=') returns Hello
In Node.js, use Buffer.from(string).toString('base64') to encode and Buffer.from(base64, 'base64').toString() to decode. The Buffer API handles all character encodings natively.
The base64 module provides encoding and decoding:
import base64
base64.b64encode(b'Hello') returns b'SGVsbG8='
base64.urlsafe_b64encode(b'Hello') for URL-safe variant
On macOS and Linux: echo -n 'Hello' | base64 to encode, echo 'SGVsbG8=' | base64 --decode to decode. On Windows PowerShell: [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes('Hello')).
Or skip the command line entirely and use our online Base64 encoder/decoder — paste text or upload a file and get instant results.
This is the most common misconception about Base64, and it is a dangerous one. Base64 is a reversible encoding — anyone can decode it without a key, password, or secret. It provides zero confidentiality.
We regularly see developers Base64-encoding passwords, API keys, or personal data and assuming it is "protected." It is not. A Base64-encoded string like cGFzc3dvcmQxMjM= can be decoded by anyone in seconds — it is just password123.
If you need to protect data:
For URL encoding needs where you want to safely encode special characters in URLs (a different problem from Base64), see our URL Encoder tool.
Encode or decode now: Base64 Encoder/Decoder — paste text, upload a file, or switch between standard and URL-safe modes. No signup required.
+ and / characters, which have special meaning in URLs. URL-safe Base64 replaces + with - and / with _, and typically omits the = padding. This makes it safe to use in URLs, query parameters, and filenames without additional encoding.← Blog index | Base64 Encoder | URL Encoder | Hash Generator | All tools