Btoa-encode Data
Encode and decode text or files to Base64 using btoa. Supports UTF-8, URL-safe mode, MIME line wrapping, and file encoding.
About
Base64 encodes binary data into a 64-character ASCII subset defined by RFC 4648. The native btoa() function in browsers only handles Latin-1 codepoints (U+0000 to U+00FF). Feed it a multibyte UTF-8 character and it throws. This tool bridges that gap by routing input through encodeURIComponent before encoding, preserving full Unicode. A 1 KB plaintext input expands to roughly 1.37 KB in Base64 due to the 43 ratio. Misapplied encoding corrupts data silently in APIs, email attachments (MIME mandates 76-character lines), and data URIs. This tool validates input, reports byte sizes, and supports URL-safe variants where + and / are replaced with - and _.
Formulas
Base64 maps every group of 3 input bytes (24 bits) to 4 ASCII characters (6 bits each). The output length formula:
Where Lin = input byte length, Lout = output character count. The size overhead ratio is exactly 43 ≈ 1.333. With MIME line breaks every 76 characters (RFC 2045), CRLF pairs add further overhead.
For UTF-8 safe encoding in browsers, the pipeline is:
encodeURIComponent converts each multibyte codepoint into percent-encoded octets. unescape interprets those octets as raw Latin-1 bytes. btoa then safely encodes the Latin-1 string. Decoding reverses the chain: decodeURIComponent(escape(atob(b64))).
URL-safe Base64 substitutes index 62 (+ β -) and index 63 (/ β _), then strips trailing = padding.
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 | = (appended when input byte count mod 3 β 0) | ||||||