Base64 Encode: Text to Base64

48 69
18537
SGk=

Base64

Swap to Base64 to text

Convert Base64 back to text

How do you encode text to Base64?

Base64 encoding rewrites your text's bytes as printable characters. Base64 exists to carry data through channels that accept only US-ASCII text. It is an encoding, not encryption: any decoder reverses it.

In short

"Hi" encodes to SGk=. Base64 reads three bytes at a time, splits the 24 bits into four six-bit groups, and writes each group as one of 64 characters; a short last group is padded with =.

  1. Read your text as UTF-8 bytes. The converter does this for you; by hand, look each character up and write its byte value.
  2. Take the bytes 3 at a time, which is 24 bits.
  3. Cut each group into 4 numbers of 6 bits.
  4. Write each number as the character at that index in the 64-character alphabet.
  5. Pad a short final group with = until it is 4 characters wide. Hi becomes SGk=.

Text to Base64 Conversion Table

Base64 writes data with 64 characters - A to Z, a to z, 0 to 9, plus (+) and slash (/) - and reserves the equals sign (=) for padding. Each row is one six-bit value and the character that stands for it, so a group's number and its character are the same lookup read in either direction. RFC 4648 s5 swaps the last two rows; the + at index 62 becomes - and the / at index 63 becomes _.

Text to Base64 Conversion Table
IndexSix bitsCharacter
0000000A
1000001B
2000010C
3000011D
4000100E
5000101F
6000110G
7000111H
8001000I
9001001J
10001010K
11001011L
12001100M
13001101N
14001110O
15001111P
16010000Q
17010001R
18010010S
19010011T
20010100U
21010101V
22010110W
23010111X
24011000Y
25011001Z
26011010a
27011011b
28011100c
29011101d
30011110e
31011111f
32100000g
33100001h
34100010i
35100011j
36100100k
37100101l
38100110m
39100111n
40101000o
41101001p
42101010q
43101011r
44101100s
45101101t
46101110u
47101111v
48110000w
49110001x
50110010y
51110011z
521101000
531101011
541101102
551101113
561110004
571110015
581110106
591110117
601111008
611111019
62111110+
63111111/

Padding in the final group

A final group of three bytes takes no padding; two bytes give three characters and one equals sign; one byte gives two characters and two equals signs. RFC 4648 requires the pad characters unless the specification that refers to it says otherwise, so a Base64 string whose length is not a multiple of four is incomplete.

Padding in the final group
Bytes in the final groupCharacters they fillPad
34none
23=
12==

The same value in each encoding

These rows are shared with the other encoders, so a value written here is the value every page in the family writes.

The same value in each encoding
ValueWritten as
Hitext 'Hi' (2 bytes) = base64 'SGk='
Mantext 'Man' (3 bytes) = base64 'TWFu'
Atext 'A' (1 byte) = base64 'QQ=='
hellotext 'hello' (5 bytes) = base64 'aGVsbG8='

Every value below is the one the specification fixes, so a result that disagrees with a row here is a result to check. ASCII defines 128 characters, codes 0–127, using 7 bits; in practice each code is stored in one 8-bit byte with a leading 0.

Base64 at a glance
AttributeValue
alphabet size64
alphabetABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
pad character=
input group bytes3
input group bits24
output group characters4
bits per character6
expansion ratio4:3
expansion percent33.33
character 62+
character 63/
output charsetUS-ASCII
padding for 1 remaining byte==
padding for 2 remaining bytes=
padding for 3 remaining bytesnone
defining specRFC 4648
mime line length76
mime defining specRFC 2045 s6.8

Worked Example: "Hi" to Base64

One group of bits at a time, with the regrouping drawn out. The same twenty-four cells are cut at eight above and at six below, which is the whole of why 3 bytes become 4 characters.

bytes
0100100001101001
sextets
010010000110100100
value
18636
Base64
SGk =

Result: Hi = SGk=

Start with the first group. Its bytes are 48 69, which is 16 bits written out in the top row. A byte is 8 bits and holds 256 values, 0–255; a nibble is 4 bits. Base64 reads three bytes (24 bits) at a time and writes them as four characters of six bits each, so the output runs four characters for every three bytes - about a third larger than the input.

The second row is the same run of bits cut every 6 instead of every eight. Each six-bit group is a number from 0 to 63, and that number is a row in the table above. Here they are 18, 6, 36, which name S, G, k. Nothing is lost in the regrouping: the same bits are read a different width, which is why the result reverses exactly.

UTF-8 encodes each character in 1 to 4 bytes. Characters 0–127 use one byte identical to ASCII, so ASCII text is valid UTF-8. That is why the byte count, not the character count, decides the length of the result — a character outside US-ASCII takes more than one byte and therefore more than one sixth of a group.

Check it in reverse

The value goes back the way it came. Four characters give their four indices, the indices give 24 bits, and the bits give 3 bytes again. Nothing is carried alongside the result: the alphabet says what each character is worth and the pad says how much of the last group was real. To verify the result by hand, decode the Base64 output back to text and compare it with what you started from.

Worked examples: text to Base64

Each example gives the value first, then the working. Click a value to load it in the converter.

What is an example of Base64?

Text "hello world" in Base64 is aGVsbG8gd29ybGQ=.

Method: read the bytes 68 65 6C as 6-bit groups and index the alphabet.

Load this value in the converter

What is "Hi" in Base64?

Text "Hi" in Base64 is SGk=.

Method: read the bytes 48 69 as 6-bit groups and index the alphabet.

Load this value in the converter

What is "Man" in Base64?

Text "Man" in Base64 is TWFu.

Method: read the bytes 4D 61 6E as 6-bit groups and index the alphabet.

Load this value in the converter

What is "A" in Base64?

Text "A" in Base64 is QQ==.

Method: read the bytes 41 as 6-bit groups and index the alphabet.

Load this value in the converter

What is "hello" in Base64?

Text "hello" in Base64 is aGVsbG8=.

Method: read the bytes 68 65 6C as 6-bit groups and index the alphabet.

Load this value in the converter

What is "&" in Base64?

Text "&" in Base64 is Jg==.

Method: read the bytes 26 as 6-bit groups and index the alphabet.

Load this value in the converter

What is "<" in Base64?

Text "<" in Base64 is PA==.

Method: read the bytes 3C as 6-bit groups and index the alphabet.

Load this value in the converter

What is "é" in Base64?

Text "é" in Base64 is w6k=.

Method: read the bytes C3 A9 as 6-bit groups and index the alphabet.

Load this value in the converter

What is a space in Base64?

Text a space in Base64 is IA==.

Method: read the bytes 20 as 6-bit groups and index the alphabet.

Load this value in the converter

What is "password" in Base64?

Text "password" in Base64 is cGFzc3dvcmQ=.

Method: read the bytes 70 61 73 as 6-bit groups and index the alphabet.

Load this value in the converter

What is "iToolHub" in Base64?

Text "iToolHub" in Base64 is aVRvb2xIdWI=.

Method: read the bytes 69 54 6F as 6-bit groups and index the alphabet.

Load this value in the converter

What is "user:pass" in Base64?

Text "user:pass" in Base64 is dXNlcjpwYXNz.

Method: read the bytes 75 73 65 as 6-bit groups and index the alphabet.

Load this value in the converter

Every example above reverses exactly - read the same four-character groups back as bytes.

What Changes the Base64 Output?

Which alphabet is in use?

This page writes the standard alphabet. The URL-safe alphabet of RFC 4648 section 5 keeps the first 62 characters and swaps plus for minus (-) and slash for underscore (_), so the result passes through a URL or a file name unchanged.

Are the line breaks part of the result?

No. RFC 4648 forbids adding line breaks to Base64 output. MIME is the specification that asks for them: RFC 2045 wraps at no more than 76 characters a line and tells decoders to ignore every character that is not in the alphabet.

What can the result be pasted into?

Anything that carries plain US-ASCII text. The alphabet was chosen so that every character in it survives a mail gateway, a JSON string and a form field without being rewritten on the way.

Is this the encoding you need at all?

The three schemes protect different things and are not interchangeable. these encodings are not interchangeable - which encoding belongs in which place sets out which does what.

Text to Base64 in Code

Both standard libraries carry this conversion, so the one-liner below is the whole job. Check the result against the table above before you trust it in a pipeline.

Python: base64.b64encode('Hi'.encode()).decode()
JavaScript: btoa(unescape(encodeURIComponent('Hi')))

Common Mistakes

Frequently Asked Questions

What is meant by Base64 encoding?

It means rewriting a run of bytes as characters from a fixed 64-character alphabet, four characters for every three bytes. The result is plain text, so anything that carries text carries it.

What is == in Base64?

It is padding. == means the final group held one byte and = means it held two, so every group stays four characters wide.

What are the disadvantages of using Base64?

Size and readability. The output runs about a third larger than the input, nobody can read the result at a glance, and it protects nothing: any decoder reverses it.

Is Base64 encryption?

No. Base64 exists to carry data through channels that accept only US-ASCII text. It is an encoding, not encryption: any decoder reverses it.

How do you Base64 encode in Python?

base64.b64encode('Hi'.encode()).decode() returns SGk=. The function takes bytes rather than a string, which is why the text is encoded to UTF-8 first and decoded back after.

"Can I go back the other way?" Yes. The same value converts straight back in the Base64 to text direction.

The rest of the family sits on the rest of the text encoding converters.