How to use
- Type or paste the text to encode, switch the input to hex to encode raw bytes, or click Open file to encode a file.
- Leave Text encoding on UTF-8 unless the receiving system expects another character set.
- Set the options: keep the Standard variant for most uses, turn padding off if the receiver does not want it, and wrap lines at 76 (MIME) or 64 (PEM) characters when needed.
- Copy the Base64 output or download it as a file.
How Base64 encoding works
The encoder reads the input three bytes at a time. Those 24 bits are split into four groups of 6 bits, and each 6-bit value (0 to 63) selects one character: A-Z for 0 to 25, a-z for 26 to 51, 0-9 for 52 to 61, then + and /. Three bytes in, four characters out, which is why Base64 is about 33% larger than the original data.
Text M a n
Bytes 77 97 110
Bits 01001101 01100001 01101110
6-bit 010011 010110 000101 101110
Index 19 22 5 46
Base64 T W F u
Padding and output length
When the input length is not a multiple of three, the last group is incomplete. One leftover byte becomes two characters followed by ==; two leftover bytes become three characters and one =. Padded output is therefore always 4 × ceil(n / 3) characters long for n input bytes.
Padding carries no data. JSON Web Tokens, URL parameters and many APIs leave it out, so turn padding off when the receiving side expects unpadded output. For links and file names, choose the URL-safe variant or use the Base64URL page, which replace + and / with - and _.
Line wrapping for MIME and PEM
By default the output is one long line, which is what JSON, data URIs and HTTP headers need. Two formats expect line breaks:
- MIME, 76 characters. Email attachments sent with
Content-Transfer-Encoding: base64must not have lines longer than 76 characters (RFC 2045). This option ends lines with CRLF, as MIME requires. - PEM, 64 characters. Certificates and keys between
-----BEGINand-----ENDlines use 64-character lines (RFC 7468). This option uses LF line endings.
Character sets and files
Base64 encodes bytes, not letters, so text is first converted to bytes with a character set. The letter é is two bytes in UTF-8 but a single byte in ISO-8859-1 or Windows-1252, so the same text produces different Base64. UTF-8 is correct for the web, JSON and nearly every modern API; pick GBK, Shift_JIS, Windows-1251 or another charset only to match a legacy system.
Files are encoded exactly as stored, with no charset conversion. For images with ready-made HTML and CSS snippets, see Image to Base64; for a data URI with the detected MIME type, use File to Base64.
Specification
| Alphabet | A-Z a-z 0-9 + / |
|---|---|
| Output size | 4 characters per 3 bytes (about 133%) |
| Padding | = to a multiple of 4 characters |
| Standard | RFC 4648 section 4 |
| Case sensitive | Yes |
Examples
| Input (UTF-8) | Output |
|---|---|
Hello, World! | SGVsbG8sIFdvcmxkIQ== |
Base64.is | QmFzZTY0Lmlz |
你好 | 5L2g5aW9 |
Frequently asked questions
Why does my Base64 output differ from another encoder?
echo adds one; use printf '%s' or echo -n), a different character set, Windows line endings in the text, or different padding, alphabet or line-wrap settings. The same bytes with the same options always give the same output.How do I Base64 encode Unicode text in JavaScript?
btoa() only accepts characters up to U+00FF and throws an error for anything else. Convert the string to UTF-8 bytes first, for example btoa(String.fromCharCode(...new TextEncoder().encode(str))). In Node.js, use Buffer.from(str, 'utf8').toString('base64').How do I Base64 encode on the command line?
printf '%s' 'your text' | base64 on Linux or macOS. GNU base64 wraps output at 76 characters by default (add -w 0 for a single line), and openssl base64 wraps at 64 (add -A).Is Base64 encoding a form of encryption?
How much larger does Base64 make my data?
Every 3 bytes become 4 characters, rounded up to a full group, so the output is about 33% larger. MIME line wrapping adds two more bytes (CRLF) per 76 characters.