← All blog posts

Base64: worked example step by step

WhiteUI · A plain-English guide to the Base64 Encoder Decoder

The fastest way to understand Base64 is to work one example all the way through, bit by bit. This guide does exactly that with the word Man, because three letters are exactly three bytes, so there is no padding to worry about in the first example. Then it repeats the same steps for Ma and M, where the padding appears, and finishes with Hello, where the input does not divide evenly. Follow along with a pen, and check each answer on the Base64 Encoder Decoder.

Man becomes TWFu: M is 77, a is 97, n is 110, and the 24 bits split into four groups of six
Man is 3 bytes: M is 77, a is 97 and n is 110. Their 24 bits split into 19, 22, 5 and 46, which read as T, W, F and u.

Step 1: letters to numbers

Each letter has a byte value in UTF-8, which for plain English letters is the same as ASCII. M is 77, a is 97, and n is 110. You can find these numbers in any ASCII table, or just remember that capital A is 65 and each letter after it adds one, so M is 65 plus 12, which is 77. Small a is 97, so a is 97 and n is 110. These three numbers are the entire input to the calculation; everything after this step is just rearranging their bits.

Step 2: numbers to bits

Write each number as 8 bits. 77 in binary is 01001101, 97 is 01100001, and 110 is 01101110. Put them side by side and you have 24 bits: 010011010110000101101110. That string of 24 bits is the data in its raw form, and the next step is the only genuinely Base64 part of the whole process.

Step 3: split into groups of six

Cut the 24 bits into four groups of 6: 010011, 010110, 000101 and 101110. Read each group as a number: 010011 is 19, 010110 is 22, 000101 is 5, and 101110 is 46. The trick to reading them is to remember the place values from the left: 32, 16, 8, 4, 2 and 1. So 010011 is 16 plus 2 plus 1, which is 19, and 101110 is 32 plus 8 plus 4 plus 2, which is 46. If a group looks wrong, add its place values again; a single wrong bit changes the letter completely.

Step 4: numbers to letters

Read each number off the Base64 alphabet: 0 to 25 are A to Z, 26 to 51 are a to z, 52 to 61 are the digits, 62 is plus and 63 is slash. 19 is the twentieth letter of the alphabet, which is T. 22 is W. 5 is F. 46 is past the capitals and the small letters start at 26, so 46 is the twenty-first small letter, which is u. Put them together and Man encodes to TWFu. That is the whole calculation: numbers, bits, groups of six, letters.

Man → TWFu
Ma → TWE=
M → TQ==
Hello → SGVsbG8=

Step 5: what changes with Ma and M

Ma is two bytes, so there are only 16 bits: 01001101 01100001. Split into groups of six, that gives 010011, 010110 and 0001, with only four bits in the last group. Two zero bits are added to make it six bits, giving 000100, which is 4, and 4 is E. So the letters are T, W and E, and one equals sign is added to mark the missing byte: TWE=. M is one byte, or 8 bits: 01001101. That gives one full group, 010011, which is 19 or T, and a leftover of 01, padded with four zero bits to 010000, which is 16 or Q. So the letters are T and Q, with two equals signs: TQ==.

A longer example: Hello

Hello is five bytes, so it makes one full group of three bytes and one leftover group of two bytes. The output is two groups of four characters, or 8 characters in total: SGVsbG8=. The single equals sign at the end shows the leftover group held two bytes. Try it yourself: the first three letters, Hel, encode to SGVs, and the last two letters, lo, encode to bG8=. A 43-letter sentence such as The quick brown fox jumps over the lazy dog is 43 bytes, which makes 15 groups and 60 characters of output.

Frequently asked questions

What is Man in Base64?

TWFu. M is 77, a is 97 and n is 110; their 24 bits split into 19, 22, 5 and 46, which read as T, W, F and u.

What is Ma in Base64?

TWE=. Two bytes give 16 bits, which split into three groups plus one = sign for the missing byte.

What is M in Base64?

TQ==. One byte gives one full 6-bit group (T) and a padded group (Q), with two = signs.

What is Hello in Base64?

SGVsbG8=. Hello is 5 bytes, so it makes 8 characters with one = at the end.

More guides on the Base64 Encoder Decoder

Try it yourself

Try the Base64 Encoder Decoder — free, in your browser.

Open the Base64 Encoder Decoder