Skip to content

What Is Base64? How the Encoding Works, with Examples

By Durim Hoti · Last updated

Computers store everything as bytes, but many formats only allow plain text: an email body, a JSON string, an HTML attribute. Base64 is the standard bridge, defined in RFC 4648. You meet it in data URLs of images, email attachments, JSON Web Tokens and HTTP Basic authentication.

How Base64 works, by hand

Base64 takes 3 bytes (24 bits), splits them into four groups of 6 bits, and replaces each group (a number from 0 to 63) with one character. The classic example is the word "Man":

Man → TWFu
Text:     M         a         n
Bytes:    77        97        110
Bits:     01001101  01100001  01101110
Groups:   010011  010110  000101  101110
Values:   19      22      5       46
Base64:   T       W       F       u

The 64 characters, in order:

Base64 alphabet
A–Z   →  0–25
a–z   →  26–51
0–9   →  52–61
+     →  62   (URL-safe: -)
/     →  63   (URL-safe: _)
=     →  padding

Encode and decode step by step

  1. Open the Base64 encoder.
  2. Type or paste the text, or click Open file. The Base64 appears on the right, with the byte and character counts above.
  3. Tick URL-safe for URLs and tokens, or Wrap at 76 for email-style line breaks.
  4. Click Copy or Download.
  5. To read Base64, paste it into the Base64 decoder.

Example: why “Grüße” needs 12 characters

"Grüße" has 5 letters, but ü and ß each take 2 bytes in UTF-8, so the text is 7 bytes. 7 bytes are two full blocks of 3 plus 1 byte left over: 4 + 4 + 4 characters, the last block padded with ==.

The Base64 encoder with the text Grüße. The counters read Text 7 bytes (UTF-8) and Base64 12 characters, and the output is R3LDvMOfZQ==. The URL-safe and Wrap at 76 options are unticked.
Grüße: 5 letters, 7 bytes in UTF-8, 12 Base64 characters.

More results from the encoder, standard and URL-safe:

TextBase64URL-safe
ManTWFuTWFu
MaTWE=TWE
MTQ==TQ
GrüßeR3LDvMOfZQ==R3LDvMOfZQ
€4oKs4oKs
😀8J+YgA==8J-YgA
<p>Hi</p>PHA+SGk8L3A+PHA-SGk8L3A-

"Ma" and "M" show the padding: one byte short gives one =, two bytes short give ==. The emoji takes 4 bytes in UTF-8. In the last row, the URL-safe version replaces + with -.

Common problems

The decoder says a character is not Base64

The decoder accepts the 64 characters, the URL-safe - and _, = at the end, spaces and line breaks, and a data: URL prefix. For SGk# the decoder says: “#” is not a Base64 character (position 4). Check for characters picked up while copying, such as quotes or a trailing period.

Padding is missing, or it uses - and _

The decoder handles both and tells you what it did:

  • TWE → "Ma": Missing padding: added 1 “=” character.
  • SGk_ → "Hi?": URL-safe Base64 (- and _) detected and decoded.

The decoded text is garbled

The Base64 contains binary data, such as an image, or text in another encoding. For images, use Base64 to image; to create image data URLs, use image to Base64.

Someone "protected" a password with Base64

Base64 protects nothing; decoding takes a second. Use real encryption or hashing for secrets.

What to do next

JSON Web Tokens are three URL-safe Base64 parts; the JWT decoder shows what is inside. Colors in CSS have their own number formats; see HEX vs RGB vs HSL. Base64 inside JSON fails to parse? See how to fix JSON errors.

FAQ

Is Base64 encryption?
No. Base64 is an encoding: anyone can decode it without a key. It makes data safe to transport as text, not secret. Never use it to hide passwords or tokens.
Why does Base64 make data larger?
Every 3 bytes become 4 characters, so the encoded text is about a third larger than the original, plus up to two = signs at the end.
What do the = signs at the end mean?
They are padding. Base64 works in blocks of 3 bytes; if the last block has only 1 or 2 bytes, the output is filled up with = so its length is a multiple of 4.
What is URL-safe Base64?
A variant that uses - and _ instead of + and /, which have special meanings in URLs, and usually drops the padding. JSON Web Tokens use it.
Is my text uploaded when I encode it?
No. Encoding and decoding run in your browser.

Try it and keep reading