CCalclabhub

Text & Writing

How To Encode Text To Base64

Base64 takes any sequence of bytes and rewrites it using only letters, digits and two punctuation marks. The point is not secrecy but safety: the output survives channels that would otherwise mangle raw binary.

Quick Answer

3 bytes -> 4 Base64 characters (with = padding to a multiple of 4)

bytes
The input measured in bytes, not characters
chars
The Base64 output length, about 4/3 of the input
=
Padding that brings the output to a multiple of four

The sample sentence is 61 UTF-8 bytes and encodes to 84 Base64 characters, a little over a third longer than the original. Hello, which is 5 bytes, becomes SGVsbG8=.

What Is How To Encode Text To Base64?

Base64 is a way of writing binary data using only printable characters. It takes groups of three bytes, which is twenty-four bits, and splits them into four groups of six bits. Each six-bit value maps to one of sixty-four symbols.

The sixty-four symbols are the twenty-six upper-case letters, the twenty-six lower-case letters, the ten digits, and the plus and slash characters. That is exactly sixty-four, which is why each one can be chosen by a six-bit number.

The encoding grows the data by about a third. Three bytes become four characters, so the output is four thirds of the input size. A one-megabyte file becomes about 1.33 megabytes once encoded.

Three bytes is the natural group size because it divides evenly into six-bit chunks. When the input is not a multiple of three bytes, the final group is padded and the output is completed with one or two equals signs so its length is always a multiple of four.

Those equals signs are padding, not data. One equals sign means the last group held two bytes, and two equals signs mean it held one. A decoder can strip and recreate the padding, which is why the URL-safe variant drops it.

The URL-safe variant replaces plus with hyphen and slash with underscore, and usually removes the padding. Plus and slash are meaningful in URLs and in file names, so the standard alphabet is awkward in those contexts. This variant is often called base64url.

Text is encoded to bytes before it is encoded to Base64. The text is first written out as UTF-8, producing a byte sequence, and only then is that sequence turned into Base64. This is why a short piece of text with an accent can produce more Base64 characters than a plain one of the same length.

Base64 is not encryption. There is no key, the transformation is reversible by anyone, and it hides nothing. It is an encoding, and the distinction matters because treating it as security is a real and common mistake.

The encoding exists because many systems carry text reliably but corrupt raw binary. Email is the classic case: the MIME standard uses Base64 so that attachments can travel through a text-only protocol without their bytes being altered.

Base64 appears in many other places. Data URIs embed small images and fonts directly in a stylesheet, JSON Web Tokens carry their header and payload as base64url, HTTP basic authentication sends a user name and password in Base64, and many APIs accept binary payloads as a Base64 string in a JSON field.

Decoding can fail. A string that is not a multiple of four characters long, or that contains a character outside the alphabet, is not valid Base64 and a strict decoder will reject it. Lenient decoders strip whitespace and repair the padding, which is why some strings decode in one tool and not another.

The line breaks in older Base64 output are a historical artefact. The MIME standard required a line break every seventy-six characters, and many encoders still insert them. Whitespace is not part of the encoding and should be removed before decoding.

The calculator encodes text to Base64 or decodes it back, with a toggle for the URL-safe alphabet. It reports the UTF-8 byte length of the input and the character length of the output, so the size overhead is visible rather than implied.

Formula

Base64 characters = 4 x ceil(bytes / 3)

Each group of three input bytes becomes four output characters, and the final partial group is padded, so the length is always a multiple of four.

SymbolMeaning
nInput bytes
LOutput length

padding = (3 - (bytes mod 3)) mod 3 equals signs

A final group of one byte takes two equals signs, a group of two takes one, and a full group takes none.

SymbolMeaning
nInput bytes
pPadding

How To Calculate How To Encode Text To Base64

  1. 1

    Convert the text to UTF-8 bytes

    Base64 works on bytes, so the text is written out as UTF-8 first. A plain ASCII letter is one byte, while an accented letter or an emoji is more.

  2. 2

    Group the bytes in threes

    Take the bytes three at a time. Each group of three bytes is twenty-four bits, which divides neatly into four six-bit chunks.

  3. 3

    Map each six-bit value to a symbol

    Each value from zero to sixty-three selects a character from the alphabet of A to Z, a to z, 0 to 9, plus and slash.

  4. 4

    Pad the final group

    If the last group held one byte, add two equals signs; if it held two, add one. This keeps the output a multiple of four characters long.

  5. 5

    Use the URL-safe alphabet where needed

    Swap plus for hyphen and slash for underscore, and drop the padding, when the result will sit inside a URL, a file name or a token.

Examples

Example 1: Encode a short string

Text
Hello
Direction
Encode
StepCalculationResult
UTF-8 bytescount the bytes5
Base64 characters4 x ceil(5 / 3)8
Base64 outputmap each six-bit groupSGVsbG8=

Result: Hello is 5 bytes and becomes SGVsbG8= in Base64, which is 8 characters including the single equals sign of padding.

Example 2: Encode text with an accented letter

Text
café
Direction
Encode
StepCalculationResult
Characterscount the characters4
UTF-8 bytesthe accented e takes two bytes5
Base64 outputmap each six-bit groupY2Fmw6k=

Result: café is 4 characters but 5 UTF-8 bytes, and it encodes to Y2Fmw6k= because the accented e needs two bytes of its own.

Calculator

Prefer a full-width tool? Open the How To Encode Text To Base64 calculator page.

Common Mistakes

  • Treating Base64 as encryption

    Base64 has no key and is trivially reversible. Anything encoded with it is readable by anyone, so it must never be used to protect a secret.

  • Encoding characters instead of bytes

    Base64 is defined over bytes. Encoding text without first converting it to UTF-8 gives the wrong result the moment a character needs more than one byte.

  • Forgetting the size overhead

    The output is about a third larger than the input. Embedding large files as Base64 data URIs inflates the page size and should be reserved for small assets.

  • Ignoring the padding

    A length that is not a multiple of four is invalid. Some decoders repair the padding automatically, which hides the error until a strict decoder rejects the string.

  • Mixing the standard and URL-safe alphabets

    A string encoded with the standard alphabet may contain plus and slash, which break when placed in a URL. Use base64url for anything that travels in a link or a file name.

  • Leaving the line breaks in

    Older encoders insert a line break every seventy-six characters. Whitespace is not part of the encoding and must be stripped before decoding.

  • Storing Base64 in a fixed-length field

    The encoded form is longer than the original, so a field sized for the raw data will overflow. Size the field for the encoded length instead.

FAQ

Is Base64 encryption?

No. It is an encoding, which means it is fully reversible by anyone and provides no secrecy. Encryption needs a key; Base64 has none. Using Base64 to hide data is a common and serious mistake.

Why does Base64 make the data bigger?

It uses four characters to represent three bytes, so the output is four thirds of the input, about thirty-three percent larger. The trade is size for compatibility with text-only channels.

What are the equals signs at the end?

They are padding that brings the output length to a multiple of four. One equals sign means the last group held two bytes, and two mean it held one. The URL-safe variant often omits them.

What is the URL-safe Base64 alphabet?

The same encoding with plus replaced by hyphen and slash replaced by underscore, and usually without padding. It is used in URLs, file names and JSON Web Tokens, where the standard characters would cause problems.

Why does my Base64 string fail to decode?

The usual causes are a length that is not a multiple of four, a character outside the alphabet, or leftover line breaks. Stripping whitespace and repairing the padding fixes most cases.

Where is Base64 used in practice?

Email attachments, data URIs that embed images and fonts in CSS, JSON Web Tokens, HTTP basic authentication, and API payloads that carry binary data inside a JSON field.

References

  1. [1]Internet Engineering Task Force, RFC 4648: The Base16, Base32 and Base64 Data Encodings — https://datatracker.ietf.org/doc/html/rfc4648
  2. [2]Wikipedia, Base64 — https://en.wikipedia.org/wiki/Base64
  3. [3]Internet Engineering Task Force, MIME and content transfer encoding — https://datatracker.ietf.org/doc/html/rfc2045