Thursday, September 20, 2012

Why do we need Base64 encoding?

We often use Base64 encoding when we need to embed an image (or any binary data) within a HTML or XML file. But why do we need to encode it as Base64? What would happen if we don't encode it as Base64?  A good discussion on this topic is available here

Base64 was originally devised as a way to allow binary data to be attached to emails as MIME. We need to use Base64 encoding whenever we want to send binary data over a old ASCII-text based protocol. If we don't do this, then there is a risk that certain characters may be improperly interpreted. For e.g.
  • Newline chars such as 0x0A and 0x0D
  • Control characters like ^C, ^D, and ^Z that are interpreted as end-of-file on some platforms
  • NULL byte as the end of a text string
  • Bytes above 0x7F (non-ASCII)
We use Base64 encoding in HTML/XML docs to avoid characters like '<' and '>' being interpreted as tags.

No comments:

Post a Comment