What is base 64 encoding used for?

Years ago, when mailing functionality was introduced, so that was utterly text based, as the time passed, need for attachments like image and media (audio,video etc) came into existence. When these attachments are sent over internet (which is basically in the form of binary data), the probability of binary data getting corrupt is high in its raw form. So, to tackle this problem BASE64 came along.

The problem with binary data is that it contains null characters which in some languages like C,C++ represent end of character string so sending binary data in raw form containing NULL bytes will stop a file from being fully read and lead in a corrupt data.

For Example :

In C and C++, this "null" character shows the end of a string. So "HELLO" is stored like this:

H E L L O

72 69 76 76 79 00

The 00 says "stop here".

Now let’s dive into how BASE64 encoding works.

Point to be noted : Length of the string should be in multiple of 3.

Example 1 :

String to be encoded : “ace”, Length=3

  1. Convert each character to decimal.

a= 97, c= 99, e= 101

enter image description here

  1. Change each decimal to 8-bit binary representation.

97= 01100001, 99= 01100011, 101= 01100101

Combined : 01100001 01100011 01100101

  1. Separate in a group of 6-bit.

011000 010110 001101 100101

  1. Calculate binary to decimal

011000= 24, 010110= 22, 001101= 13, 100101= 37

  1. Covert decimal characters to base64 using base64 chart.

24= Y, 22= W, 13= N, 37= l

“ace” => “YWNl”

enter image description here

Example 2 :

String to be encoded : “abcd” Length=4, it's not multiple of 3. So to make string length multiple of 3 , we must add 2 bit padding to make length= 6. Padding bit is represented by “=” sign.

Point to be noted : One padding bit equals two zeroes 00 so two padding bit equals four zeroes 0000.

So lets start the process :–

  1. Convert each character to decimal.

a= 97, b= 98, c= 99, d= 100

  1. Change each decimal to 8-bit binary representation.

97= 01100001, 98= 01100010, 99= 01100011, 100= 01100100

  1. Separate in a group of 6-bit.

011000, 010110, 001001, 100011, 011001, 00

so the last 6-bit is not complete so we insert two padding bit which equals four zeroes “0000”.

011000, 010110, 001001, 100011, 011001, 000000 ==

Now, it is equal. Two equals sign at the end show that 4 zeroes were added (helps in decoding).

  1. Calculate binary to decimal.

011000= 24, 010110= 22, 001001= 9, 100011= 35, 011001= 25, 000000=0 ==

  1. Covert decimal characters to base64 using base64 chart.

24= Y, 22= W, 9= j, 35= j, 25= Z, 0= A ==

“abcd” => “YWJjZA==”


It's basically a way of encoding arbitrary binary data in ASCII text. It takes 4 characters per 3 bytes of data, plus potentially a bit of padding at the end.

Essentially each 6 bits of the input is encoded in a 64-character alphabet. The "standard" alphabet uses A-Z, a-z, 0-9 and + and /, with = as a padding character. There are URL-safe variants.

Wikipedia is a reasonably good source of more information.


When you have some binary data that you want to ship across a network, you generally don't do it by just streaming the bits and bytes over the wire in a raw format. Why? because some media are made for streaming text. You never know -- some protocols may interpret your binary data as control characters (like a modem), or your binary data could be screwed up because the underlying protocol might think that you've entered a special character combination (like how FTP translates line endings).

So to get around this, people encode the binary data into characters. Base64 is one of these types of encodings.

Why 64?
Because you can generally rely on the same 64 characters being present in many character sets, and you can be reasonably confident that your data's going to end up on the other side of the wire uncorrupted.