Base64 Encoder/Decoder
Encode and decode text using Base64 encoding. Supports URL-safe Base64 format.
Text to Encode
Base64 Output
Input Length
13
Output Length
20
Size Ratio
153.8%
About Base64: Base64 encodes binary data into ASCII characters using 64 symbols (A-Z, a-z, 0-9, +, /). It increases size by ~33% but ensures safe text transmission.
What Is Base64 Encoding?
Base64 is a binary-to-text encoding scheme that converts arbitrary binary data into a string of printable ASCII characters. The name comes from the fact that the encoding uses exactly 64 distinct characters: the 26 uppercase letters AβZ, the 26 lowercase letters aβz, the 10 digits 0β9, and the two symbols + and /. A special padding character = is appended when needed to make the output length a multiple of four.
Base64 was designed to solve a fundamental compatibility problem in data transmission. Many communication protocols and file formats were originally built around 7-bit ASCII text, meaning raw binary bytes above 127 β or even special control characters β could be corrupted, stripped, or misinterpreted during transport. By representing every three bytes of binary input as four printable ASCII characters, Base64 guarantees that data travels safely through any text-only channel, including email servers, XML documents, JSON APIs, and HTML attributes.
It is critical to understand that Base64 is an encoding, not an encryption. The transformation is entirely reversible and provides zero confidentiality. Anyone with a Base64 decoder can retrieve the original data instantly without any key or password. Use Base64 for safe transport and storage, never as a security measure.
How Base64 Encoding Works
The encoding process works on groups of three bytes at a time. Each group of 24 bits is divided into four 6-bit chunks. Each 6-bit value (ranging from 0 to 63) is then mapped to one character in the Base64 alphabet table. If the input byte count is not a multiple of three, one or two = padding characters are appended to complete the final group to four output characters.
This calculator uses the browser-native btoa() and atob() functions combined with encodeURIComponent and unescape to handle Unicode text correctly. Standard btoa() only accepts Latin-1 (ISO 8859-1) strings, so the encoding pipeline first calls encodeURIComponent() to percent-encode multibyte Unicode characters into safe byte sequences, then unescape() converts those percent-encoded sequences into a Latin-1 compatible string that btoa() can safely process.
| Index Range | Characters | Description |
|---|---|---|
| 0 β 25 | A β Z | 26 uppercase letters |
| 26 β 51 | a β z | 26 lowercase letters |
| 52 β 61 | 0 β 9 | 10 digits |
| 62 | + | Plus (standard) or β (URL-safe) |
| 63 | / | Slash (standard) or _ (URL-safe) |
| pad | = | Padding to reach a multiple of 4 |
Base64 Encoding Formula
Where:
- text= The input string to encode
- encodeURIComponent(text)= Percent-encodes non-Latin-1 Unicode characters to safe byte sequences
- unescape(...)= Converts percent-encoded sequences back to raw Latin-1 bytes, making input safe for btoa()
- btoa(...)= Browser-native function that performs standard Base64 encoding on a Latin-1 string
- byteLength= The number of bytes in the Latin-1 byte string passed to btoa()
- outputLength= The total character count of the Base64 output, always a multiple of 4 including = padding
URL-Safe Base64 Variant
Standard Base64 uses the characters + (plus) and / (slash) as the 62nd and 63rd alphabet characters. Both carry special meaning in URLs: a plus sign in a query string is interpreted as a space, and a forward slash is a path separator. This makes raw Base64 strings unreliable when embedded directly in URLs or HTTP query parameters without additional percent-encoding.
The URL-safe Base64 variant, defined in RFC 4648 Β§5, solves this by substituting + with - (hyphen) and / with _ (underscore) β two characters that have no special meaning in URLs. Padding = characters are also stripped because they can interfere with some URL parsers.
This calculator applies the URL-safe transformation by running .replace(/+/g, '-').replace(///g, '_').replace(/=+$/, '') on the standard Base64 output. When decoding URL-safe input, the tool reverses the substitution (-β+, _β/), then re-adds = padding until the string length is a multiple of four before calling atob(). URL-safe Base64 is the standard encoding for JWT tokens, OAuth 2.0 tokens, and URL-embedded binary parameters.
Common Use Cases for Base64
Base64 encoding appears throughout modern software development in contexts where binary data must be transported or stored as text. Knowing where it is applied helps you use it correctly in your own projects.
- HTTP Basic Authentication: Credentials are formatted as
username:passwordand Base64-encoded before being placed in theAuthorization: Basic ...header. For example, admin:password encodes to YWRtaW46cGFzc3dvcmQ=. - Data URLs in HTML and CSS: Small images, fonts, or other binary assets can be embedded directly in markup as
data:image/png;base64,...URIs, eliminating additional HTTP requests for tiny resources. - Email attachments (MIME): The MIME standard uses Base64 to encode binary attachments so they travel safely through legacy email infrastructure that only supports 7-bit ASCII transport.
- JSON API payloads: When a REST API must carry binary data β such as image bytes, audio clips, or cryptographic keys β inside a JSON body, Base64 encoding converts that binary into a safe JSON string value.
- JWT tokens: Each part of a JSON Web Token (header, payload, signature) is Base64url-encoded, making the full token URL-safe and human-readable when decoded.
- Cryptographic key files: Public and private keys stored in PEM format use Base64 to represent binary key material as readable text that terminals and editors can safely display.
Size Overhead and Performance Considerations
Base64 encoding increases data size by approximately 33.3%. This is because every 3 bytes of input become 4 ASCII characters of output β an expansion factor of 4/3 β 1.333. The size ratio shown by this calculator is computed directly from the encoded and decoded lengths: in encode mode the ratio is (outputLength / inputLength) Γ 100, and in decode mode it is (inputLength / outputLength) Γ 100, both expressed as a percentage and rounded to one decimal place.
For small data items such as short tokens, API keys, or credential strings, the 33% overhead is negligible. For large binary files such as images or audio, however, the size increase has meaningful consequences: a 1 MB image becomes roughly 1.37 MB when Base64-encoded. Embedding large images as data URLs in HTML is generally discouraged for this reason, as the inflated payload increases page-load time and HTTP transfer costs.
Modern HTTP compression (gzip or Brotli) applied on top of Base64-encoded text can recover much of the overhead, because Base64 output has predictable, compressible character patterns. Most web servers compress responses automatically, mitigating the size penalty for network transfer. However, in-memory footprint and on-disk storage are still affected. For performance-critical applications, consider alternatives such as binary protocol buffers or multipart form-data for large binary payloads, reserving Base64 for the specific interoperability contexts where it is genuinely required.
Worked Examples
Encode a Plain Text String
Problem:
Encode the string "Hello, World!" to Base64.
Solution Steps:
- 1Input text: "Hello, World!" β 13 characters of pure ASCII.
- 2Apply encodeURIComponent() to percent-encode any non-Latin-1 characters (none here, so string is unchanged).
- 3Apply unescape() to collapse percent-encoded sequences into raw Latin-1 bytes for btoa().
- 4Apply btoa() to perform standard Base64 encoding on the Latin-1 byte string.
- 5Size ratio (encode mode): (20 / 13) Γ 100 = 153.8%.
Result:
Output: SGVsbG8sIFdvcmxkIQ== | Input length: 13 | Output length: 20 | Size ratio: 153.8%
Encode HTTP Basic Auth Credentials
Problem:
HTTP Basic Authentication requires the string "admin:password" to be Base64-encoded for the Authorization header.
Solution Steps:
- 1Format the credentials as "admin:password" β 14 characters.
- 2Pass through the encoding pipeline: encodeURIComponent β unescape β btoa.
- 3Standard Base64 output is "YWRtaW46cGFzc3dvcmQ=" β 20 characters.
- 4Size ratio (encode mode): (20 / 14) Γ 100 = 142.9%.
Result:
Output: YWRtaW46cGFzc3dvcmQ= | Input length: 14 | Output length: 20 | Size ratio: 142.9%
Decode a Base64 String Back to Text
Problem:
Decode the Base64 string "SGVsbG8=" to recover the original text.
Solution Steps:
- 1Input Base64 string: "SGVsbG8=" β 8 characters, including 1 padding = character.
- 2Call atob("SGVsbG8=") to perform standard Base64 decoding, producing the raw byte string "Hello".
- 3Apply escape() then decodeURIComponent() to safely reconstruct any multi-byte Unicode sequences from the decoded bytes.
- 4Final decoded text: "Hello" β 5 characters. Size ratio (decode mode): (8 / 5) Γ 100 = 160.0%.
Result:
Output: Hello | Input length: 8 | Output length: 5 | Size ratio (decode mode): 160.0%
Tips & Best Practices
- βEnable the URL-safe toggle whenever your encoded string will appear in a URL, query parameter, JWT token, or filename to avoid conflicts with reserved characters.
- βBase64 provides no security β always encrypt sensitive data with a proper algorithm such as AES before encoding it for transport.
- βThe = padding at the end of a Base64 string tells you how many bytes the final input block contained: one = means 2 bytes, two == means 1 byte, no = means an exact multiple of 3 bytes.
- βUse the swap button to reverse the operation β it copies the output back to the input and flips the mode from encode to decode (or vice versa) in one click.
- βA valid Base64 string always has a length that is a multiple of 4. If yours does not, padding characters may have been stripped, which is common in URL-safe tokens.
- βWhen embedding a small Base64 image in HTML, use the data URL format: <code>data:image/png;base64,YOUR_DATA_HERE</code> in the src attribute. Reserve this technique for small icons only.
- βFor large binary files, multipart form-data or direct binary uploads are far more efficient than Base64, which adds approximately 33% size overhead.
- βBase64 is case-sensitive β uppercase and lowercase letters represent different values, so always preserve the exact casing of encoded strings when copying or storing them.
Frequently Asked Questions
Sources & References
Last updated: 2026-06-05
Help us improve!
How would you rate the Base64 Encoder/Decoder?
Editorial Note
MyCalcBuddy Editorial Team
This page is maintained as an educational calculator reference.
Formula Source: Standard Mathematical References
by Various