How to convert text to Base64
- Type or paste the string into Input · text. Whatever you enter is encoded exactly, including spaces and line breaks.
- The Base64 appears in Output · Base64. The Input figure below the buttons shows how many bytes your text became.
- Leave Charset on UTF-8 unless the system that will decode the value expects something else.
- Tick URL-safe (- _) and No padding (=) if the string goes into a URL or a token.
- Press Copy output, or Download to save it as
base64.txt.
To go back, use Base64 to text, or press Swap to decode the result right here.
One string, several possible Base64 values
Base64 encodes bytes, and a string becomes bytes only through a charset. That is why the same five letters can produce four different answers:
| String | Charset | Bytes | Base64 |
|---|---|---|---|
Grüße | UTF-8 | 7 | R3LDvMOfZQ== |
Grüße | Windows-1252 or ISO-8859-1 | 5 | R3L832U= |
Grüße | UTF-16LE | 10 | RwByAPwA3wBlAA== |
Grüße | UTF-16BE | 10 | AEcAcgD8AN8AZQ== |
Invisible characters count as well. Hello is SGVsbG8=, Hello with a trailing space is SGVsbG8g, and Hello followed by a Windows line ending is SGVsbG8NCg==. Line endings are a classic trap in multi-line values: line1 and line2 joined by \n encode as bGluZTEKbGluZTI=, joined by \r\n as bGluZTENCmxpbmUy. If two systems disagree about a value, compare the byte counts first.
How long the Base64 string will be
Every 3 bytes of input become 4 characters, and a final group of 1 or 2 bytes is padded to 4 with =.
| Input bytes | Base64 characters | Example (x repeated) |
|---|---|---|
| 1 | 4 | eA== |
| 2 | 4 | eHg= |
| 3 | 4 | eHh4 |
| 4 | 8 | eHh4eA== |
| 10 | 16 | eHh4eHh4eHh4eA== |
So a string grows by about a third, and by more if non-ASCII characters take several bytes each in UTF-8. The Base64 size overhead page has the exact numbers for wrapped output.
Where encoded strings end up
People convert strings to Base64 mostly to move them through places that dislike special characters:
- Environment variables and CI secrets, where a value with quotes,
$or line breaks would be mangled.postgres://app:s3cret@db:5432/appbecomescG9zdGdyZXM6Ly9hcHA6czNjcmV0QGRiOjU0MzIvYXBw, one line with nothing to escape. - Kubernetes Secrets, whose
data:values must be Base64. The Kubernetes Secret decoder reads them back. - HTTP headers, for example
Authorization: Basicwithuser:passwordencoded. The Basic Auth header generator builds the full header. - JSON and XML fields that must carry arbitrary text without escaping rules getting in the way (JSON to Base64).
None of these hide the value. Anyone with the string can decode it, so treat an encoded secret as a plain secret; see encoding vs encryption.
String to Base64 in code
import base64
base64.b64encode("Grüße".encode("utf-8")).decode("ascii") # 'R3LDvMOfZQ=='
base64.b64encode("Grüße".encode("cp1252")).decode("ascii") # 'R3L832U='
// Node.js
Buffer.from("Grüße", "utf8").toString("base64"); // "R3LDvMOfZQ=="
Buffer.from("Grüße", "utf16le").toString("base64"); // "RwByAPwA3wBlAA=="
Both languages need the string-to-bytes step spelled out, which is where the charset choice lives. More on the Python and Node.js pages.
FAQ
How do I convert a string to Base64?
Turn the string into bytes with a charset (UTF-8 almost everywhere), then Base64-encode the bytes. The tool above does both; in Python it is base64.b64encode(s.encode("utf-8")).decode().
Does the same text always give the same Base64?
Only if the bytes are the same. Grüße is R3LDvMOfZQ== in UTF-8 but R3L832U= in Windows-1252, and one extra space or newline changes the result too.
How long will the Base64 string be?
4 × ⌈n / 3⌉ characters for n bytes of input, before any line breaks. A 10-byte string gives 16 characters; the size calculator works it out for any length.
Can I put a Base64 string in a config file or environment variable?
Yes, that is one of its main uses: it turns any value, including line breaks and quotes, into a single line of safe characters. Keep it on one line by leaving Line wrap set to None.