Base64 Encoder

Turn text or a file into Base64. You need it whenever part of the stack only accepts ASCII: a Basic Auth header, a data URI, a mail attachment. Encoding runs in this tab, so a credential you paste here is not sent anywhere.

Input Text
Base64 Output

1 Base64 Encoder example

Input Text

1
2
Hello World!
This is a test for Base64 Encoder.

Base64 Output

1
SGVsbG8gV29ybGQhClRoaXMgaXMgYSB0ZXN0IGZvciBCYXNlNjQgRW5jb2Rlci4=

2 When to use the Base64 Encoder

Text-only channels

Encode bytes before they cross a text-only channel. SMTP, JSON string fields and HTTP headers carry ASCII safely but can corrupt raw binary. Base64 writes every 3 bytes as 4 ASCII characters, so the data arrives intact.

Data URIs

Encode a small icon and paste it into a stylesheet as a data: URL instead of making another request. Keep this for files of a few KB, since the encoded string is about 33% larger than the file.

Non-ASCII input

Accented characters and emoji are converted to UTF-8 bytes before encoding, so they decode back unchanged. For example, é becomes the two bytes C3 A9.

How to use the Base64 Encoder tool

  1. Type or paste text into the left editor. The Base64 output appears on the right as you type.
  2. To encode a file, click Upload File and pick a text file; its contents are loaded into the editor.
  3. Click Load URL to fetch text from an endpoint that allows cross-origin requests.
  4. Click Copy Result to copy the encoded string. The status bar under each panel shows the size, so you can see the 33% growth.
  5. Dummy Data loads a sample with accented characters and emoji, which shows why btoa() alone is not enough. Clear resets both panels.

How to Base64 encode a string in JavaScript, PHP and Python

JavaScript / Node.js

const text = "Hello, wörld! 🌍";

// Node.js: Buffer handles any Unicode string
const encoded = Buffer.from(text, "utf8").toString("base64");
console.log(encoded); // SGVsbG8sIHfDtnJsZCEg8J+MjQ==

// Browser: btoa() only accepts Latin-1, so UTF-8 encode first
const bytes = new TextEncoder().encode(text);
const browserEncoded = btoa(String.fromCharCode(...bytes));
console.log(browserEncoded === encoded); // true

// URL-safe variant (RFC 4648 section 5), no padding
console.log(Buffer.from(text).toString("base64url"));

String.fromCharCode(...bytes) throws on very large inputs; loop in chunks or use Uint8Array.prototype.toBase64() where available.

PHP

<?php
$text = "Hello, wörld! 🌍";

$encoded = base64_encode($text);
echo $encoded, PHP_EOL; // SGVsbG8sIHfDtnJsZCEg8J+MjQ==

// URL-safe variant: swap + and /, strip = padding
$urlSafe = rtrim(strtr($encoded, '+/', '-_'), '=');
echo $urlSafe, PHP_EOL;

// Encode a file, for example an image
// $encoded = base64_encode(file_get_contents('logo.png'));

Python

import base64

text = "Hello, wörld! 🌍"

# b64encode works on bytes, so encode the string as UTF-8 first
encoded = base64.b64encode(text.encode("utf-8")).decode("ascii")
print(encoded)  # SGVsbG8sIHfDtnJsZCEg8J+MjQ==

# URL-safe variant uses - and _ instead of + and /
print(base64.urlsafe_b64encode(text.encode("utf-8")).decode("ascii"))

# From the shell:  echo -n "hello" | base64

Without -n, echo appends a newline, so the output ends in Cg== and does not match what other tools produce.

Common errors and how to fix them

InvalidCharacterError: The string to be encoded contains characters outside of the Latin1 range
btoa() only accepts code points 0 to 255. Convert the string to UTF-8 bytes with TextEncoder first, as in the JavaScript snippet above.
Encoded output ends in Cg== or CG== and does not match another tool
Your input has a trailing newline, usually from echo or a file saved by an editor. Use echo -n or printf, or strip the newline before encoding.
Base64 breaks a URL or query string
Standard Base64 uses + / and =, which have meaning in URLs. Use the base64url variant, or percent-encode the value.
The receiving side rejects the string because of line breaks
Some encoders (MIME, openssl base64) wrap output at 64 or 76 characters. Remove the newlines, or pass -A to openssl to get a single line.

Good to know

Every 3 input bytes become 4 output characters, drawn from a 64-symbol alphabet (A-Z, a-z, 0-9, + and /). That ratio is where the 33% growth comes from. If the input length is not a multiple of 3, = padding fills out the last group, which is why so many encoded strings end in one or two equals signs. The spec is RFC 4648.

The same RFC defines base64url, which swaps + and / for - and _ so the value survives in URLs, filenames and JWTs. Padding is usually dropped there too. However, a decoder for one alphabet rejects the other. Be sure to check which one the receiving side expects before you ship, because a token that looks fine but will not decode is usually in the wrong alphabet.

Specifications and references

This tool follows the specifications below. Read them when you need the exact normative wording, for example to settle an edge case between two implementations.

Guides and tutorials

Frequently Asked Questions

What is Base64 used for?

Base64 represents binary data as ASCII text so it can pass through systems that expect text: JSON, XML, email, HTTP Basic authentication headers and data URIs.

Does it handle Unicode and emoji?

Yes. Text is converted to UTF-8 bytes before Base64 encoding, so any character works and decodes back correctly. For example, é encodes as w6k=.

Is Base64 encryption?

No. Anyone can reverse Base64 without a key, so it offers no security. Use it to move data through text-only channels, and use encryption when the data has to stay secret.

Why is the output longer than the input?

Base64 writes every 3 bytes as 4 characters, so the output is about 33% larger, plus up to two = padding characters. For example, a 30 KB file becomes about 40 KB of text.

Can I encode a file?

Yes. Click Upload File to load a file and encode its contents. For images, use Image to Base64, which also produces a ready-to-use data URI.

Going the other way

To read an encoded string, use the Base64 decoder. For pictures specifically, Image to Base64 produces a data URI you can paste into CSS.

More Encoders & Decoders

Browse all encoders & decoders

Copied to clipboard!