Base64 Encode
Base64 encoder: type text or choose a file and get its Base64. Text is encoded as UTF-8. Choose the standard or URL-safe alphabet, padding on or off, and MIME line wrapping. The size before and after is shown. Nothing is uploaded.
How Base64 encoding works: three bytes in, four characters out
Base64 turns any sequence of bytes into text made of 64 safe characters. It takes the input three bytes at a time, which is 24 bits, cuts those 24 bits into four groups of six, and writes each group as one character from a table of 64: the capital letters, the lower case letters, the digits, + and /. Six bits can hold 64 values, which is why the table has 64 entries, and why the output is four characters for every three input bytes.
The text Man is the classic example. M, a and n are the bytes 4D, 61 and 6E, which are 01001101, 01100001 and 01101110. Joined, the 24 bits are 010011 010110 000101 101110, and the four six-bit values 19, 22, 5 and 46 pick the characters T, W, F and u from the table. Man becomes TWFu, and the diagram shows each step.
The encoder above performs the same steps on whatever you give it: the text you type, converted to UTF-8 bytes, or a file you choose. Everything runs in the browser, and the result is shown with its length and how much larger it is than the input.
How much bigger does Base64 make data?
Every three bytes become four characters, so Base64 text is about a third larger than the data it holds (4 ÷ 3 = 1.33). When the length is not a multiple of three, padding rounds up. The exact output length for n input bytes is 4 × ⌈n ÷ 3⌉. MIME wrapping adds a carriage return and a line feed after every 76 characters, which is about 2.6 percent more.
| Input bytes | Output characters | Growth |
|---|---|---|
| 1 | 4 | 300% |
| 2 | 4 | 100% |
| 3 | 4 | 33% |
| 6 | 8 | 33% |
| 100 | 136 | 36% |
| 1,000 | 1,336 | 34% |
| 1,024 | 1,368 | 34% |
| 1,048,576 | 1,398,104 | 33% |
The first rows show how padding dominates for tiny inputs: one byte becomes four characters, 300 percent. The last row is a mebibyte, which becomes 1,398,104 characters. A 5 MB attachment in an email is therefore close to 6.7 MB on the wire, and a JSON response that carries a Base64 image is a third bigger than the image file. If the transport compresses text, gzip recovers most of the difference for compressible data, but Base64 of already-compressed data such as a JPEG only shrinks to about three quarters of its text size, which brings it back near the original.
Standard, URL-safe and MIME output compared
There is one idea and several small variations. The alphabets differ in two characters, and the formats differ in padding and line length. Choosing the wrong one is a common source of decode errors on the receiving side, so check what the destination expects.
| Variant | Alphabet | Padding | Line breaks | Defined in |
|---|---|---|---|---|
| Standard | A–Z a–z 0–9 + / | Yes, = to a multiple of 4 | None | RFC 4648 section 4 |
| URL-safe | A–Z a–z 0–9 - _ | Optional; often omitted | None | RFC 4648 section 5 |
| MIME | A–Z a–z 0–9 + / | Yes | At most 76 characters, CRLF | RFC 2045 |
| PEM | A–Z a–z 0–9 + / | Yes | 64 characters, between BEGIN and END lines | RFC 7468 |
The encoder offers standard and URL-safe alphabets, padding on or off and MIME wrapping at 76 characters. The 60-byte string of the letter A, wrapped in MIME style, comes out as 2 lines: the first 76 characters long, then 4. PEM files use the same alphabet as MIME but wrap at 64, so select the standard alphabet and break the output yourself if a tool needs that exact layout.
Encoding Unicode text: why btoa throws an error
Base64 encodes bytes, and text has to become bytes first. The encoder uses UTF-8, which is what virtually every service expects. A euro sign, for example, is one character but three bytes (E2 82 AC), and its Base64 is 4oKs. A plain ASCII letter is one byte, so most English text looks the same in UTF-8 and in older single-byte encodings, which hides the issue until an accent appears.
JavaScript's built-in btoa function predates Unicode handling. It treats each character as one byte and throws an InvalidCharacterError for anything above U+00FF, so btoa("€") fails. Writing the text out as UTF-8 first fixes it, and the code at the end of this page shows how. The reverse problem exists too: atob returns Latin-1 characters, so decoded UTF-8 text must be decoded again. When the receiver expects another encoding, such as UTF-16 for a Windows service, the bytes differ and so does the Base64: the same € is rCA= in UTF-16 little-endian and IKw= in big-endian, against 4oKs in UTF-8.
Encoding files and binary data
Use the Choose file button to encode a file. It is read in the browser and never uploaded, and the result is the Base64 of the file's exact bytes. This is how images, PDFs and small documents are embedded in JSON, XML, HTML or CSS. For images there is a dedicated converter, image to Base64, that also writes data URIs and CSS.
Large files need care. The whole file is held in memory, and the output is a third larger, so a 200 MB file needs several hundred megabytes of browser memory. Streaming encoders avoid that by processing the input in chunks, with one rule: each chunk except the last must have a length that is a multiple of three. Otherwise padding appears in the middle, and the pieces cannot simply be joined. On the command line, base64 file handles any size.
Base64 in QR codes and barcodes
A QR code has several modes. Numeric mode packs digits densely and alphanumeric mode covers capital letters, digits and a few symbols, but Base64 uses lower case letters, which are in neither, so a Base64 payload is stored in byte mode at eight bits per character. Because only six of those bits carry data, a third of the capacity is lost. Binary data fits better when it is written directly in byte mode, with no Base64 at all, if the scanner software passes the bytes through unchanged.
For text-only channels with the same limitation, a dedicated encoding exists: Base45, defined in RFC 9285, uses exactly the 45 characters of the QR alphanumeric set and was designed for QR codes, as in the European digital health certificates. It is longer than Base64 as text but denser inside the code. For a simple link or token, URL-safe Base64 in a short URL is usually enough.
Padding: when to keep it and when to drop it
The = characters are only filler. They bring the length to a multiple of four, which is useful for streaming decoders that work in groups, and they are redundant because the length of the unpadded text tells a decoder the same thing. Keep them for MIME, PEM, XML and JSON documents, where a strict parser may insist. Drop them in URLs, JWTs and cookies, where = has meaning or needs escaping.
The risk of dropping them is on the receiving side: Python's standard decoder raises a padding error, and some libraries need the length restored. If you do not know what the receiver accepts, send the padded form; every decoder reads it. The tool above has a padding switch, and the decoder on the Base64 decode page restores missing padding itself.
Base64 in HTTP Basic authentication
Basic authentication joins the user name and password with a colon, encodes the result in Base64 and sends it in the Authorization header. For user and pass the text is user:pass, the Base64 is dXNlcjpwYXNz, and the header reads Authorization: Basic dXNlcjpwYXNz. The example in the original specification, Aladdin:open sesame, becomes QWxhZGRpbjpvcGVuIHNlc2FtZQ==.
This is an encoding for transport and offers no secrecy: anyone who sees the header can read the credentials with a decoder, so Basic authentication is only acceptable over HTTPS. The same applies to Base64 in configuration files and Kubernetes secrets, which are encoded and not encrypted.
Base64 in email, JSON, XML and certificates
Email was the original use. SMTP was designed for 7-bit text, so attachments are encoded with the header Content-Transfer-Encoding: base64 and wrapped at 76 characters, as RFC 2045 describes. JSON has no binary type, so APIs send images, signatures and documents as Base64 strings in a field. XML does the same with the xsd:base64Binary type. Certificates and keys in PEM files are Base64 of DER bytes between BEGIN and END lines.
Data URIs in HTML and CSS embed small files directly, and tokens such as JWTs carry their parts in URL-safe Base64. The common thread is a channel that cannot be trusted with arbitrary bytes: quotes, control characters and line breaks inside binary data would be misread, and Base64 uses only characters that survive every layer.
When not to use Base64
Base64 solves a transport problem and creates a size problem, so it is the wrong choice when the channel can carry bytes. Upload large files with multipart form data or a binary request body, which has no one-third overhead. Serve images as files so browsers can cache and decode them on their own thread; an image embedded in a stylesheet is downloaded again whenever the stylesheet changes. Store binary values in a binary column and not as Base64 text, which wastes a third of the space and costs time to convert.
And do not use it to hide anything. A token or password that is only Base64-encoded is plain text with extra steps. For secrecy, encrypt with a vetted library first, and only then use Base64 to carry the ciphertext. The comparison with the other common byte-to-text encoding is on the hexadecimal page.
Base64 encode in JavaScript, Python, Java, PHP and the shell
// Node.js
Buffer.from("Hello, World!", "utf8").toString("base64") // 'SGVsbG8sIFdvcmxkIQ=='
Buffer.from([0xfb, 0xff]).toString("base64url") // '-_8'
// Browser: encode UTF-8 bytes, not the characters
btoa(String.fromCharCode(...new TextEncoder().encode("€"))) // '4oKs'import base64
base64.b64encode("Hello, World!".encode("utf-8")) # b'SGVsbG8sIFdvcmxkIQ=='
base64.urlsafe_b64encode(b"\xfb\xff") # b'-_8='
base64.b64encode(b"\xfb\xff").decode().rstrip("=") # strip padding by handjava.util.Base64.getEncoder().encodeToString("Hello".getBytes(java.nio.charset.StandardCharsets.UTF_8));
java.util.Base64.getUrlEncoder().withoutPadding().encodeToString(bytes);
base64_encode("Hello");
System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes("Hello"));printf 'Hello' | base64 # SGVsbG8=
base64 -w0 file.bin # GNU: no line wrapping; wraps at 76 by default
[Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes("Hello"))The shell commands show the usual trap: echo Hello | base64 encodes a trailing line feed, so the result is SGVsbG8K and not SGVsbG8=. Use printf or echo -n when the line feed should not be part of the data.
Worked examples: one, two and three bytes
The three padding cases are easiest to see with short strings. Hi! is three bytes, 48, 69 and 21, which is exactly one group: its 24 bits split into 010010, 000110, 100100 and 100001, the values 18, 6, 36 and 33, and the characters S, G, k and h. The result is SGkh, with no padding.
Hello is five bytes, one full group and a short one. The first three bytes (Hel) give SGVs. The remaining two bytes, 6C and 6F, are 16 bits, which the encoder pads with two zero bits to reach 18, three six-bit groups: 011011, 000110 and 111100, the values 27, 6 and 60, written b, G and 8. One = fills the group to four characters, and the result is SGVsbG8=. A single byte is worse: A is 41, 01000001, padded to 12 bits as 010000 and 010000, the characters Q and Q, and two = signs follow, giving QQ==. Padding always works out as the number of missing bytes.
Encoding a number: first decide what the bytes are
Base64 encodes bytes, so a number has to be turned into bytes before it can be encoded, and there are several ways to do it. The text 255 is three characters (32 35 35 in UTF-8 with the digits as ASCII), the value 255 as one byte is FF, and as a 32-bit integer it is four bytes whose order depends on the convention. Each gives a different Base64 string.
| What is encoded | Bytes | Base64 |
|---|---|---|
| The text 255 | 32 35 35 | MjU1 |
| One byte, value 255 | FF | /w== |
| 32-bit integer, big-endian | 00 00 00 FF | AAAA/w== |
| 32-bit integer, little-endian | FF 00 00 00 | /wAAAA== |
| The text FF (hex digits as characters) | 46 46 | RkY= |
This is the usual explanation when a Base64 value does not match one produced elsewhere. If an API expects the integer, send the bytes in the order it documents, and if it expects text, send the text. The tool above encodes text; for raw bytes, enter them as hex in the hex to Base64 converter, which goes from bytes to Base64 directly.
Base64 in URLs, query strings and file names
The standard alphabet has three characters that are special in URLs: + (a space in form data), / (a path separator) and = (a separator in query strings). Putting standard Base64 in a URL needs percent-encoding of those characters, which turns a Base64 value into a longer, ugly string; the URL-safe alphabet avoids the problem by design. The value a/b+c= is escaped as a%2Fb%2Bc%3D, and if the escaping is forgotten, a + arrives as a space and the string fails to decode.
The same applies to file names, where / is illegal and some systems also reject +. Choose the URL-safe alphabet and drop the padding for names, tokens in links and values in cookies, and make sure the receiver uses the matching decoder. If the receiver is a standard decoder that cannot be changed, keep the standard alphabet and escape the three characters.
Line breaks in the output
Plain Base64 is one unbroken line. MIME wrapping breaks it into lines of at most 76 characters with a carriage return and line feed after each, which email requires. The line break is not part of the data, and decoders skip it. An accidental line break, from a text editor that wraps long lines or a terminal that inserts one, is the usual cause of a string that fails in a decoder that does not accept whitespace.
Command-line tools differ: GNU base64 wraps at 76 characters unless told otherwise with -w 0, whereas macOS versions of the tool historically did not wrap. When a hash or token is produced with a command and compared with another, wrapping is the first thing to check. The output of this tool is a single line unless MIME wrapping is switched on.
Size limits of the places Base64 ends up
The one-third growth matters where there is a hard limit. HTTP servers commonly limit a header to 8 KB, which leaves room for about 6 KB of data in Base64, so tokens and cookies that grow can fail. Kubernetes limits a secret to 1 MiB, counting the Base64 text, so the data itself is about 750 KB. JSON bodies, API gateways and message queues have their own caps on request size, and these apply to the encoded size.
Plan with the encoded length: 4 × ⌈n ÷ 3⌉ characters for n bytes, or ⌈4n ÷ 3⌉ without padding. A 6 KB limit holds 4,500 bytes. When the limit is the problem, compress the data first (gzip or deflate, then Base64), which often more than recovers the overhead for text and JSON, or send the data by another route and pass only a reference.
The output is deterministic, and where it varies
Base64 has no randomness and no key. The same bytes always give the same string with the same options, which is why it appears in checksums and signed requests. The strings of two correct encoders differ only in the places the standard leaves open: the alphabet (standard or URL-safe), the padding, the line wrapping and the case of the = sign, which has none. When two Base64 values for the same data do not match, compare them after removing the padding and replacing - and _ with + and /, and the cause is usually visible.
Compress first, then encode
Base64 cannot make data smaller, but compression applied before it can more than pay for the overhead when the data is repetitive. Text, JSON and XML compress well; images, archives and video are already compressed and gain nothing. The comparison below is computed on a 1,200-byte string of repeated text when this page is built, using gzip.
| Form | Bytes | Base64 characters |
|---|---|---|
| Original text | 1200 | 1600 |
| After gzip | 97 | 132 |
The receiver has to know to decompress after decoding, so the format needs a marker: a field name, a content type or a prefix. A decoded string that begins with the bytes 1F 8B is gzip data, and in Base64 it starts with H4sI. For data that is not repetitive, such as random keys or already compressed files, skip the compression step.
Secrets, passwords and tokens
Encoding a secret in Base64 hides nothing, as the Basic authentication example shows, and it is a common mistake to treat configuration values as protected because they look scrambled. The correct order for a secret that has to travel in text is to encrypt it with a vetted algorithm and a key, and only then encode the ciphertext in Base64 so it survives the channel. The decoder then reverses the two steps in the opposite order. Random tokens that must be unguessable are generated from a cryptographic random source and encoded for display, which is a good use of Base64 and URL-safe Base64 in particular.
Mistakes that give the wrong Base64
- Encoding a trailing line feed. Text from a file or a command often ends in a newline, which changes the output at the end.
- Encoding the characters of a number or of a hex string when the bytes were meant: the text FF is not the byte FF.
- Encoding text with the wrong character encoding, so that a receiver expecting UTF-8 reads garbage for every non-ASCII character.
- Calling btoa on a string with characters above U+00FF, which fails, or on a string that was already decoded as UTF-8.
- Using the standard alphabet where a URL-safe one is needed, or the reverse, without telling the receiver.
- Stripping padding for a decoder that requires it, or adding it where a parser rejects it.
- Encoding a file that was read in text mode, which on some platforms rewrites line endings and corrupts binary data.
The seven test vectors in RFC 4648 settle most disagreements about an implementation, and they are listed with their results on the Base64 reference page.
Questions about Base64 encoding
Why does Base64 output end with = or ==?
Base64 works in groups of three bytes. When the data does not fill the last group, the output is padded with = signs to a multiple of four characters: one = when the last group had two bytes, two when it had one. The padding carries no data.
How do I encode Unicode text such as é or an emoji?
The text is first converted to bytes with UTF-8 and then to Base64, which is what this encoder does. In JavaScript, calling btoa directly fails for characters above U+00FF; convert with TextEncoder first.
What is the difference between Base64 and Base64URL?
Base64URL replaces + with - and / with _ so the output can sit in a URL or a file name without escaping, and it usually omits the = padding. The data encoded is identical.
Is Base64 a form of encryption?
No. It changes how bytes are written, with no key, and anyone can decode it. It is used to carry binary data through systems that only handle text, not to protect it.
Is there a size limit for what I can encode?
The encoder has no fixed limit, but the file or text is held in browser memory, so very large files are limited by your device. Files of a few hundred megabytes are slow in any browser tool; use the command line for those.
