Touchbase with Base64

Touchbase with Base64

A clean way to understand base64 conversion strategy.

What is base64?

Base64 is a method of representing binary data in an ASCII string format. This is achieved by converting a binary data into 6-bit character representation.

Base64 uses a specific algorithm for encoding which helps to convert the binary data into the desired output. This is a reversible algorithm, and we can retrieve the original data from a Base64 string by back-pedalling (reversing the algorithm).

Let us see an example

Let’s have a quick run-through on how this algorithm works. Just for fun we will convert the word

“Base.64”

into a Base64 string.

The very first thing that we would want to do is split the word into separate characters. So we have 6 characters n our selected word:

Base.64

Now let’s convert each of the above into their corresponding binary values, you can also use the Base64 Index Table if you want:

01000010011000010111001101100101001011100011011000110100

Now we need to form a single string out of the above by concatenating them one after the other. The resulting string will be as following:

01000010011000010111001101100101001011100011011000110100

Once we have done this, we have to carefully break it again so that each part has 6 characters. Something like this:

010000100110000101110011011001010010111000110110001101000000

(Note: If your last part is less than 6 characters, append it with zeros. That'll do fine. Here we added 4 zeros “0000” to make it 6-bit bytes.) Now each part is of 6-bit bytes, prepend with “00” and convert each part to 8-bit bytes: 00010000 00100110 00000101 00110011 00011001 00010010 00111000 00110110 00001101 00000000 Now it’s time to convert them to their corresponding decimal values:

163855125185654130

The above list of integers you have received are called Base64 indices. You can map the Base64 indices to the Base64 Character Table and form your final Base64 string. So Let’s do that real quick:

QmFzZS42NA

Now, do you remember we added 4 zeros to the last part in the 4th step?

So for every two zeros we will append 1 padding character “=”.

Voila! Now you have your final Base64 string for the word “Base64”.

QmFzZS42NA==

Cool! Don’t you think?

Now to decode a Base64 string you have to perform the same steps but backwards, if you do that correctly you will reach the actual string. I would suggest that you give it a try! 😉