Roopak Neevan's Blog

Base64 Encoding

Posted by: Naveen Kapoor on: January 9, 2009

Base64 Encoding :  Base64 encoding is a use to encodes arbitrary binary data as a string composed from a set of 64 characters. The character constitute of a 64 distinct ASCII characters,

 ”A” -  “Z”

“a” -  “z”

“0″ -  “9″

“+” , “/”

 For example, using this character set the 40-bit data:

 01001000 01100101 01101100 01101100 01101111 cBase64 encoding for above binary data is : SGVsbG8=

Where “=” character is a padding character.

 Why Base64 Encoding.

 To transmit binary data on the communication channel that is designated to transmit the character data, like Emails. In case user want a binary image thru mail, User can apply base64 encoding on image Binary data and transmit the same thru mail. As Base64 encoding use only 64 character and these 64 characters can be represented with just 6 Bits

 How it works:

 lets take the above example :

 Initals Binary Data:

01001000 01100101 01101100 01101100 01101111 ……………

 To Result Bytes

XX010010  XX000110  XX010101  XX101100 …………………..

Final Result Chars

    S                                                   t     ……………………..

 Encoding Process :

  • Divid the input bytes stream into blocks of 3 bytes.
  • Divid the 24 bits of a 3-byte block into 4 groups of 6 bits.
  • Map each group of 6 bits to 1 printable character, based on the 6-bit value.
  • If the last 3-byte block has only 1 byte of input data, pad 2 bytes of zero (\x0000). (As we need to make the bits count to 24 so that we can divide it in 4 groups of 6 bits) After encoding it as a normal block, override the last 2 characters with 2 equal signs (==), so the decoding process knows 2 bytes of zero were padded.
  • If the last 3-byte block has only 2 bytes of input data, pad 1 byte of zero (\x00). (As we need to make the bits count to 24 so that we can divide it in 4 groups of 6 bits)  After encoding it as a normal block, override the last 1 character with 1 equal signs (=), so the decoding process knows 1 byte of zero was padded.
  • Carriage return (\r) and new line (\n) are inserted into the output character stream. They will be ignored by the decoding process.

You can refer :

Leave a Reply


  • mudassir: thanks ............. .................... did a great job for guiding
  • Anand V: This is an wonderful effort !! It really helps lots of parents to understand the procedures and shed light over the minor passport myths. My hea
  • Deepak Dandriyal: Very Good MAN thanks for sharing,,,,,,,,,,god bless