What is Base64? What is Base64 Encoding Used For?
We talked about what encoding is in the previous article already. You can read the following article if you’re unfamiliar with encoding:
What is Base64 encoding?
Base64 is an encoding scheme. It’s used for many reasons, but you’ve probably heard that it’s used for data transmission way. We’ll investigate how Base64 Encoding works.
What did it serve to be created for?
When computer science started to develop, each computer had a data process protocol. Many different encoding techniques were created, which used a different number of bits per character. So inconsistency will start at that point. Some computers store binary data in bytes consisting of 8 bits each, so ASCII is unsuitable for transferring this type of data. Furthermore, some systems would even wipe the most significant bit. Thus somehow, computers shouldn’t interrupt communication. There had to be a way to handle this encoding inconsistency.
The name of that way is Base64. This allows you to encode these complicated bytes to bytes that are known to be safe to send without getting corrupted. To send text safely, you can first encode to bytes using a text encoding of your choice (for example, UTF-8), and then afterward, Base64 encodes the resulting binary data into a text string that is safe to send encoded as ASCII. The receiver will have to reverse this process to recover the original message.
So with the power of Base64, computers can communicate with computers that use 8 bits system between ASCII-7.
But here is an important breakthrough UTF-8. If you’d like to see another article about it, I’d love to write it.
Disadvantage
Base64 try to encode the 3 bytes to 4 ASCII character. So, Base64 increases its length.
How Does Base64 Work?
Let’s investigate by solving a sample. One step at a time.
We chose a word which is “try”.
Step 1: After then, we need to find the binary values for all letters. To do that, we need to use the ASCII table.
Step 2: And, we got the ASCII table and right away we can find the values. And we separated all the letters into binary values.
Step 3: We wrote all these values side by side. This time we divided them into groups of 6.
Step 4: Calculate the binary values. And we can find the values according to the Base64 Indexing table. And here we are.
Basically, we learned how Base64 Encoding works. Now there might be a problem when the number of characters to be encoded does not come with a multiple of six bits, zeros will be used to complete the last six-bit sequence. We say it is a padding value which is ‘ =’.
Why do we need to add padding?
If we knew the length of the stream beforehand, we wouldn’t need to add padding. But in the scenario that we don’t know the stream length, It’s a pretty reasonable solution. But still, the receiver should know which charset is used, etc.
How to Add Padding to Base 64?
It is mandatory for a proper conversion into Base64 that the resulting data must be converted into sequences of 24 bits each. However, at times, it happens that this length is not satisfied, i.e., a few bits might not be there, or the total bits of the encoded data are fewer than 24. So that’s why need to add padding.
Let’s investigate a sample about padding too.
When we investigate the base64 encoding of the letter “t”, as you can see, the list is truncated at 8. bytes. To resolve this issue, we added zeros until the bytes count can divide into 24. If the entire array consists of appended zeros, we converted this into ‘=’ Therefore, the receiver can understand the “=” characters have no meaning it’s just padding and the end of the stream.
References