What is the advantage of using Base64 encoding?

What is the advantage of using Base64 encode?

I would like to understand it better. Do I really need it? Can't I simply use pure strings?

I heard that the encoding can be up to 30% larger than the original (at least for images).


Solution 1:

Originally some protocols only allowed 7 bit, and sometimes only 6 bit, data.

Base64 allows one to encode 8 bit data into 6 bits for transmission on those types of links.

Email is an example of this.

Solution 2:

The primary use case of base64 encoding is when you want to store or transfer data with a restricted set of characters; i.e. when you can't pass an arbitrary value in each byte.

Solution 3:

<img alt="Embedded Image" 
  src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIA..." />

This code will show encoded image, but no one can link to this image from another website and use your traffic.

Base64 decode

Solution 4:

The advantages of Base64 encode, like somebody said, are available to transmit data from binary, into (most commonly) ASCII characters. Due to the likeliness that the receiving end can handle ASCII, it makes it a nice way to transfer binary data, via a text stream.

If your situation can handle native binary data, that will most likely yield better results, in terms of speed and such, but if not, Base64 is most likely the way to go. JSON is a great example of when you would benefit from something like this, or when it needs to be stored in a text field somewhere. Give us some more details and we can provide a better tailored answer.

Solution 5:

One application is to transfer binary data in contexts where only characters are allowed. E.g. in XML documents/transfers. XML-RPC is an example of this.