← All blog posts

Base64 practice questions with answers

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

Reading about Base64 is useful, but doing it is what makes it stick. This post has ten practice questions with full answers, covering encoding, decoding, sizes, padding and spotting broken strings. Try each question before you read its answer, and check your work on the Base64 Encoder Decoder. If you get one wrong, the explanation says where the slip usually happens.

Three practice answers: Hi is SGk=, QmFzZTY0 decodes to Base64, and 100 bytes become 136 characters
Three questions to try first: encode Hi, decode QmFzZTY0, and work out what 100 bytes become.

Questions 1 to 3: encoding short words

Question 1: encode Hi. Hi is two bytes, so it makes one group of four characters with one equals sign: SGk=. The usual slip is forgetting the padding, so check the length is four characters and the last one is =. Question 2: encode Yes. Yes is three bytes, so it divides exactly into one group with no padding: WWVz. The slip here is adding an equals sign out of habit, so check the byte count first: three bytes need no padding. Question 3: encode sure. Sure is four bytes, which is one full group plus one leftover byte, so the answer is 8 characters with two equals signs: c3VyZQ==. Work it out in two parts if it helps: sur encodes to c3Vy, and e on its own encodes to ZQ==.

Questions 4 to 6: decoding

Question 4: decode QmFzZTY0. Six characters with no padding means two full groups, or six bytes, and the answer is Base64. Question 5: decode V2hpdGVVSQ==. Twelve characters with two equals signs means the last group held one byte, and the answer is WhiteUI. Question 6: decode SGVsbG8=. Eight characters with one equals sign means the last group held two bytes, and the answer is Hello. The pattern to learn is that the padding tells you the shape of the answer before you decode it: no padding means the byte count divides by three, one = means two bytes are left over, and two = signs mean one byte is left over.

Questions 7 and 8: sizes

Question 7: how many characters is the Base64 of 100 bytes? Use the formula: output length is 4 times the ceiling of 100 divided by 3. 100 divided by 3 is 33.33, rounded up to 34 groups, times 4 gives 136 characters. Question 8: how many characters is the Base64 of 1,000 bytes? 1,000 divided by 3 is 333.33, rounded up to 334 groups, times 4 gives 1,336 characters. The point of both questions is that you can predict the size without encoding anything, and that the overhead settles at about a third: 136 is 36% bigger than 100, and 1,336 is 33.6% bigger than 1,000.

Questions 9 and 10: spot the broken string

Question 9: which of these is not valid Base64 — SGVsbG8=, SGVsbG8, or SGVs!G8=? The second is broken, because it is 7 characters long and the length is not a multiple of four, which means the padding was dropped. The third is broken too, because the exclamation mark is not in the Base64 alphabet, so it cannot decode at all. Only SGVsbG8= is valid, and it decodes to Hello. Question 10: encode the sentence The quick brown fox jumps over the lazy dog, which is 43 bytes. 43 divided by 3 is 14.33, rounded up to 15 groups, times 4 gives 60 characters, and the answer is VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZw==, with two equals signs because one byte is left over after 14 full groups.

Check your answers

If you scored ten out of ten, you understand the whole method: three bytes become four characters, the padding marks the leftover bytes, and the output length follows the formula. If you missed the size questions, practise the formula with your own numbers: 10 bytes give 16 characters, 11 bytes give 16, and 12 bytes give 16 as well, because 12 divides exactly into four groups. If you missed a padding question, remember the table: one byte left over gives ==, two bytes give =, and three bytes give nothing. And if you missed a decoding question, decode the answer again by hand once: turn each letter into its number, join the bits, split them into bytes, and read the letters. Doing it once by hand is worth more than reading about it ten times.

Frequently asked questions

What is Hi in Base64?

SGk=. Hi is two bytes, so it makes four characters with one = sign at the end.

What does QmFzZTY0 decode to?

Base64. Six characters with no padding means two full groups, or six bytes.

How many characters is the Base64 of 100 bytes?

136. 100 divided by 3 is 33.33, rounded up to 34 groups, times 4 gives 136.

How do I spot a broken Base64 string?

Check the length is a multiple of four, the = signs are only at the end, and every character is in the alphabet. SGVsbG8 without its = fails the first check.

More guides on the Base64 Encoder Decoder

Try it yourself

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

Open the Base64 Encoder Decoder