Base 64, Part 2: Padding

Well, Part 1 of this blog explained about the basics of base 64, here I would like to discuss more about base 64.

Base 64 Padding

I’m starting from example rather explaining in words. In Part 1 I took 3 letter word ‘man’ to explain, now I am taking a 4 letter word ‘many’. Lets do it again,

  • The binary representation of each letter is
    m 01101101
    a 01100001
    n 01101110
    y 01111001
  • Now, group 6-bits, you will get five groups of 6-bits and two bits remaining. See the following table.
    011011 010110 000101 101110 011110 01
  • Now, how do you get base 64 alphabet for the last 2-bit group? This is where the padding helps. To get exactly 6-bits in each group, keep increasing the binary string (filled with zeroes) to its right hand side. See the following table after increasing 1 byte The newly added bits are in green color.
    011011 010110 000101 101110 011110 010000 0000
  • Here also, the last group is not 6-bits, so add one more byte, resulting in the following table.
    011011 010110 000101 101110 011110 010000 000000 000000

    Now, we are exactly getting 6-bits in each group.

This is why padding is needed in base 64 whenever the base 64 string is not divisible exactly groups of 6-bits each. Well, the next step is find the base 64 alphabet for each group. Take the equivalent decimal value of each group.

bit groups 011011 010110 000101 101110 011110 010000 000000 000000
equivalent decimal (ie. index) 27 22 5 46 30 16 [pad] [pad]
encoded value b W F u e Q = =

Permanent link to this article: https://blog.openshell.in/2012/10/base-64-padding/

Leave a Reply

Your email address will not be published.