Base64 Decode Algorithm
The Base64 decode algorithm converts plain text into original data. Technically, it can be said that it converts six-bit bytes into eight-bit bytes. To understand how the decoding algorithm works, check the example below that describes step by step how to manually decode strings from Base64 (if you are looking for an automatic converter, use the Base64 online decoder).
For example, you have the “QUJD” string and want to decode it from Base64:
- First, you need to split the string letter by letter. Thus, you got 4 groups:
Q
U
J
D
- Each group (letter) is a Base64 character that has its own index, and now your task is to convert groups to indices. To do this, by mapping values from the Base64 Characters Table replace each character by its index (if you cannot find an index for a specific group, just discard it). All in all, you should get the following indices:
16
20
9
3
- At this step you should convert each group from decimal to binary. So find corresponding decimal values in the ASCII table and make sure you get the following binary values:
00010000
00010100
00001001
00000011
- Now remove the prefix “00” (two zeros) in front of each group:
010000
010100
001001
000011
- There you have a simple concatenation of previous groups (that is, glue all the binary values together and get an 24-character string):
010000010100001001000011
- Then, divide the resulting string into groups so that each one has 8 characters (if the last group has less than 8 characters, you must discard it). Now you have 3 groups of eight-bit bytes:
01000001
01000010
01000011
- Once again using the ASCII table, convert all binary values into their ASCII characters:
A
B
C
- The final chord, concatenate all ASCII characters to get the result string:
ABC
By the way, if the decoding result contains multibyte characters most likely the last two steps may confuse you a little bit since some “groups” may look garbled. This is because individually each byte represents an ASCII symbol and you won’t know what kind of data it yields until you combine all bytes together. Therefore, if you got some weird results at the seventh step and want to understand better what’s happening here, use this ASCII converter to combine and convert binary numbers obtained on the sixth step (just keep in mind, that, for example, four binary numbers may be one character, two characters, and even a single character).
To summarize, you learned that decoding “QUJD” from Base64 yields the result “ABC”. As you can see, this is a very simple process and you can decode Base64 even by hand. I hope that you managed to get the right decoding result. Otherwise, let me know and I will try to help you.
If you need more step by step examples, use the form below to get decoding instructions for custom strings (once you submit the form, the article above will be updated accordingly in real time):
Comments (13)
I hope you enjoy this discussion. In any case, I ask you to join it.
I updated the article and also I added the ability to get decoding instructions for custom strings. Please give it a try and let me know if you still have any questions.
I have a base64 string.. Now that base64 string has 4 txt files in it with their filenames. Those 4 txt files have comma separated header and data...
So in other words if I goto base64 to file decode at your site and put my string into it.. It give me a zipped file to download which has 4 files..
I want to know.. How come this decoding is happening..
You said.. We can manualy decode base64 too.. I don't understand how can we decode such a base64 string.. Thanks...
iVBORw0KGgoAAAANSUhEUgAAAMkAAAAjAQAAAAAWYhNMAAAALklEQVR4XmPQfSdyfdm3uKqz0md515t2bhOOu3ybW988XaSBYVRqVGpUapBJAQD/Y6EOyJKSeQAAAABJRU5ErkJggg==
It's nice to meet you here. I have a question, however- if a binary code has less than two zeroes on the beginning, what should I do? Anyways, thanks a lot!
How you convert the binary values to ascii?
Thanks!
i just can't imagin how could this happen.as the base64-encoding algorithm,the string should always be divided evenly by 8
By the way, in fact a Base64 string length should be a multiple of 4, not 8.