Base64 Encode String
Encode any text string to Base64 online. Supports UTF-8, URL-safe encoding (RFC 4648), MIME line wrapping, and instant copy or download.
About
Base64 encoding converts arbitrary binary data into a restricted set of 64 ASCII characters (A - Z, a - z, 0 - 9, +, /) plus = for padding. Every group of 3 input bytes maps to 4 output characters, producing an output size ratio of exactly 43. Encoding a raw Unicode string without first converting it to UTF-8 bytes is the single most common source of corruption in data pipelines, API payloads, and email attachments. This tool performs proper UTF-8 byte serialization before applying the Base64 alphabet, so multi-byte characters (emoji, CJK, Cyrillic) encode correctly.
Two encoding variants are supported. Standard Base64 follows RFC 4648 ยง4 and is used in MIME email and PEM certificates. URL-safe Base64 follows RFC 4648 ยง5, replacing + with - and / with _, and stripping = padding, which prevents breakage in query strings and filenames. Choosing the wrong variant will cause parse failures downstream. This tool approximates output size instantly and lets you toggle MIME line wrapping at 76 characters per RFC 2045.
Formulas
Base64 processes input as a stream of bytes. Each group of 3 bytes (24 bits) is split into 4 sextets (6 bits each). Each sextet indexes the Base64 alphabet table above.
Where nout = number of output Base64 characters (including padding), and nbytes = byte length of the UTF-8 encoded input. The overhead ratio converges to 43 ≈ 1.333 for large inputs.
Bit extraction for sextet k from a 24-bit group G:
Where k โ {0, 1, 2, 3} and 0x3F = 63 masks the lower 6 bits.
For URL-safe encoding (RFC 4648 ยง5), two substitutions are applied to the output alphabet:
Reference Data
| Character | Index | Character | Index | Character | Index | Character | Index |
|---|---|---|---|---|---|---|---|
| A | 0 | Q | 16 | g | 32 | w | 48 |
| B | 1 | R | 17 | h | 33 | x | 49 |
| C | 2 | S | 18 | i | 34 | y | 50 |
| D | 3 | T | 19 | j | 35 | z | 51 |
| E | 4 | U | 20 | k | 36 | 0 | 52 |
| F | 5 | V | 21 | l | 37 | 1 | 53 |
| G | 6 | W | 22 | m | 38 | 2 | 54 |
| H | 7 | X | 23 | n | 39 | 3 | 55 |
| I | 8 | Y | 24 | o | 40 | 4 | 56 |
| J | 9 | Z | 25 | p | 41 | 5 | 57 |
| K | 10 | a | 26 | q | 42 | 6 | 58 |
| L | 11 | b | 27 | r | 43 | 7 | 59 |
| M | 12 | c | 28 | s | 44 | 8 | 60 |
| N | 13 | d | 29 | t | 45 | 9 | 61 |
| O | 14 | e | 30 | u | 46 | + | 62 |
| P | 15 | f | 31 | v | 47 | / | 63 |
| Padding character: = - appended when input byte length is not divisible by 3 | |||||||
Frequently Asked Questions
btoa() function only accepts Latin-1 (ISO 8859-1) characters, where each code point fits in a single byte (range 0 - 255). Characters outside this range (emoji, CJK, Cyrillic, Arabic) have code points above 255 and cause an InvalidCharacterError. The correct approach is to first encode the string to UTF-8 bytes using TextEncoder, then Base64-encode those bytes. This tool performs that conversion automatically.