A virtual teacher who reveals to you the great secrets of Base64

Base64 Encode Algorithm

The Base64 encode algorithm converts any data into plain text. Technically, it can be said that it converts eight-bit bytes into six-bit 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).

Let’s say, you have ABC string and want to convert it to Base64:

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

  2. 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, you have 3 groups of 8 characters each:
    • 01000001
    • 01000010
    • 01000011

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

  4. Then, divide the resulting string into groups so that each one has 6 characters (that is, now you have 4 groups):
    • 010000
    • 010100
    • 001001
    • 000011

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

  6. 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

  7. Integer numbers obtained in the previous step are called “Base64 indices”. They are easy to remember, because it is a zero-based 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

  8. 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 I hope that you managed to get the right encoding result. Otherwise, let me know and I will try to help you.

Comments (1)

I hope you enjoy this discussion. In any case, I ask you to join it.

  • Ian,
    Hi there,

    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
Add new comment

If you have any questions, remarks, need help, or just like this page, please feel free to let me know by leaving a comment using the form bellow.
I will be happy to read every comment and, if necessary, I will do my best to respond as quickly as possible. Of course, spammers are welcome only as readers.

Loading...