Base64 Encode Algorithm
The Base64 encode algorithm converts any data into plain text. Technically, it can be said that it converts eightbit bytes into sixbit bytes. To understand how the encoding algorithm works, check the example below that describes step by step how to manually encode strings to Base64 (if you are looking for an automatic converter, use the Base64 online encoder).
For example, you have the “ABC” string and want to convert it to Base64:

First, you need to split the string letter by letter. Thus, you got 3 groups:
A
B
C

Next you need to convert each group to binary. To do this, for each letter you need to find the corresponding binary value in the ASCII table. Thus, now you have 3 groups of ones and zeros:
01000001
01000010
01000011

Now concatenate all the binary values together (that is, glue all the groups along and make sure you get a total of 24 characters):
010000010100001001000011

Then, divide the resulting string into groups so that each one has 6 characters (if the last group has less than 6 characters, you need to fill it with zeros until it reaches the desired length). Well and good, now you have 4 groups:
010000
010100
001001
000011

At this step you have to convert sixbit bytes into eightbit bytes. To do this, prepend the prefix “00” (two zeros) in front of each group:
00010000
00010100
00001001
00000011

There you have to convert each group from binary to decimal by finding its corresponding decimal value in the ASCII table. If you did everything right, each group will be transformed into its integer number as follows:
16
20
9
3

Integer numbers obtained in the previous step are called “Base64 indices”. They are easy to remember, because it is a zerobased numbering, where each index corresponds to a Latin letter. It starts with the letter “A” in alphabetical order (i.e., A=0, B=1, C=2, D=3, and so on). For complete list, see Base64 Characters Table. So, matching indexes, convert them to corresponding letters:
Q
U
J
D

The final chord, concatenate all letters to get the Base64 string:
QUJD
To summarize, you learned that encoding “ABC” to Base64 yields the result “QUJD”. As you can see, this is a very simple process and you can encode text to Base64 even by hand. I hope that you managed to get the right encoding 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 encoding instructions for custom strings (once you submit the form, the article above will be updated accordingly in real time):
Comments (17)
I hope you enjoy this discussion. In any case, I ask you to join it.
I am stuck in encoding from hex to base64 in Android. Would you please show me how? I tried
Base64.encode(hexString.getBytes(), Base64.DEFAULT); and Base64.encodeToString but am not successful yet.
Best regards,
Ian
https://stackoverflow.com/questions/7372268/howtoconverthextobase64
rtrim()
function). If you plan to replace it (=
) with another character after encoding, you must reverse the process before decoding.Convert stuff lower or equals to 127 is fine, but how about higher than that? lets say the character '¤', according to ASCII Table the corresponding binary value is `10100100` (which is 145 in decimal) but in here I got `1100001010100100`, what's happening here?
how do you convert any character that has a decimal value higher than 127?
Thank you for your message and I'm sorry for confusing you.
The problem is that the extended ASCII table (decimal > 127) and the encoder provided on this page use different character encodings. Instead of the extended ASCII table, please use the converter provided on the ASCII Table page.
Meanwhile I'll try to figure out how to make it clear for everyone (perhaps the best option is to remove the extended ASCII table, because it can also be confusing at decoding).
By the way, it doesn’t matter the character encoding as long as you use the same character set for encoding and decoding. For example, if you use Windows1252 table to encode
A¤B
to Base64 you’ll get theQaRC
result. And you can decode it back to the original string if you still use Windows1252. But if you try to decodeQaRC
using the CP850 character set, it will be decoded asAñB
. For more differences, try to decodeQaRC
using the Character Encoding Detection tool.So I've been really interested in the Advanced Encryption Standard, but it's kind of hard because I'm not the best coder ever. When I've discovered Base64, I've been really interested in it. But is there a way to encode with a password kind of like AES (Advanced Encryption Standard)? I read that Base64 isn't the best encoding algorithm as people can easily decode it.
Thanks