Base64 Explained: Encode and Decode Text and Images
Last updated November 2, 2026
Base64 turns any data (text, images, files) into plain letters and digits so it can travel safely through systems built for text: email, JSON, HTML, URLs and configuration files. This guide explains how it works, its common uses and pitfalls, and how to encode or decode it in the browser, in code and on the command line.
How Base64 works
Base64 takes the data three bytes (24 bits) at a time and writes them as four characters from a 64-character alphabet: AโZ, aโz, 0โ9, + and /. When the input is not a multiple of three bytes, = pads the end. Because 3 bytes become 4 characters, Base64 output is about 33% larger than the original.
Example: Hello encodes to SGVsbG8=. Text is first turned into UTF-8 bytes, so Hรฉllo ๐ becomes SMOpbGxvIPCfkYs=.
Base64 is not encryption
Anyone can decode Base64 instantly; it hides nothing. Never use it to "protect" passwords or personal data. If you find Base64 in a config file or token, assume the contents are readable by anyone who has the file.
Where you meet Base64
- Data URLs:
data:image/png;base64,iVBORw0โฆembeds an image directly in HTML or CSS. - Email attachments (MIME) are Base64-encoded behind the scenes.
- APIs and JSON often carry files or binary data as Base64 strings.
- HTTP Basic authentication sends
username:passwordin Base64, which is why it must only be used over HTTPS. - JWT tokens use the URL-safe variant (below) for their header and payload.
Standard vs URL-safe Base64
+ and / have special meanings in URLs and file names, so the URL-safe variant replaces them with - and _ and often drops the = padding. The bytes FB FF encode to +/8= in standard Base64 and -_8= in URL-safe Base64. If a decoder rejects a string containing - or _, swap them back to + and / first.
Encode or decode online
- Base64 Encoder: paste text and get standard Base64. Unicode and emoji are encoded as UTF-8.
- Base64 Decoder: paste Base64 to get the text back. Spaces and line breaks in the input are ignored, and invalid input shows "Invalid Base64" instead of garbage.
- Image to Base64: choose an image to get a complete data URL. PNGs stay PNG; JPG and WebP images are re-encoded as JPG.
- Base64 to Image: paste a complete data URL (starting
data:image/png;base64,, or jpeg, webp, gif) to preview and download the image. If you only have the Base64 part, add that prefix in front.
All four run in your browser, so tokens and images are not sent to Uvolix.
In code and on the command line
| Where | Encode | Decode |
|---|---|---|
| JavaScript (browser) | btoa('Hello') (Latin-1 text only) | atob('SGVsbG8=') |
| JavaScript, any Unicode | Convert with new TextEncoder() / TextDecoder before and after. btoa only understands Latin-1: it encodes รฉ as a single non-UTF-8 byte and throws an error on emoji | |
| Node.js | Buffer.from('Hello').toString('base64') | Buffer.from(s, 'base64').toString() |
| Python | base64.b64encode('Hello'.encode()).decode() | base64.b64decode(s).decode() |
| Linux / macOS | echo -n 'Hello' | base64 | echo 'SGVsbG8=' | base64 --decode |
| Windows PowerShell | [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes('Hello')) | [Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($s)) |
Use echo -n on Linux and macOS: without it the trailing newline is encoded too and you get SGVsbG8K.
Common problems
- "Invalid character" errors: the string was cut off, contains URL-safe characters, or has extra text such as the
data:prefix. - Garbled accents after decoding: the bytes were decoded with the wrong character set; decode as UTF-8.
- Huge HTML pages: data URLs are 33% bigger than the image and cannot be cached separately. Inline only small icons.
FAQ
Why does Base64 end with = or ==?
Padding: one = when the last group had two bytes, two when it had one.
Can Base64 be decoded without a key?
Yes. It is an encoding, not encryption.
Is Base64 safe to put in a URL?
Use the URL-safe variant, or percent-encode the standard one. See the URL encoding guide.
More developer tools are on the developer tools page.
Encode text to Base64.