You can refer to this example and see how base64 decoding works:
I have also attached the reference to base64 table.
Base64 index table
Decoding->base64 string : QWJoaXNoZWs=
First, you need to split the string character by character. Thus, you got 12 groups:
Q
W
J
o
a
X
N
o
Z
W
s
=
Each group (character) 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
22
9
40
26
23
13
40
25
22
44
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
00010110
00001001
00101000
00011010
00010111
00001101
00101000
00011001
00010110
00101100
Now remove the prefix “00” (two zeros) in front of each group:
010000
010110
001001
101000
011010
010111
001101
101000
011001
010110
101100
There you have a simple concatenation of previous groups (that is, glue all the binary values together and get an 66-character string):
010000010110001001101000011010010111001101101000011001010110101100
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 8 groups of eight-bit bytes:
01000001
01100010
01101000
01101001
01110011
01101000
01100101
01101011
Once again using the ASCII table, convert all binary values into their ASCII characters:
A
b
h
i
s
h
e
k
The final chord, concatenate all ASCII characters to get the result string:
Abhishek
Thus,
Size of original string(in bytes) = floor(6n/8) – padding
Size of base64 string(in bytes) = ceil(8n/6) + padding
When decoding from base64
int paddingCount = (base64Data.endsWith("==")) ? 2 :(base64Data.endsWith("=")) ? 1 : 0;
double dataSize = floor(base64Data.length() * 3 / 4) - paddingCount;
When encoding to base64
int paddingCount = 3 - (stringToEncode.length()) % 3;
double dataSize = ceil(stringToEncode.length() * 4 / 3) + paddingCount;