49

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

3
  • 1
    Base64 encoding emits 4 bytes of printable data for every 3 bytes of binary data. If you also put newlines in periodically (eg, mail programs limit the line length to 64 or 72 characters), then you get one more byte 48 or 54 input bytes for the newline (unless you play with CRLF line endings when you get 2 bytes per line). Nov 5, 2009 at 20:24
  • 6
    For disadvantages, look here stackoverflow.com/questions/14418785
    – Val
    Jan 19, 2013 at 21:13
  • Make use of this link for clear understanding. stackoverflow.com/a/201510/3979414
    – Kumar
    Feb 17, 2016 at 13:02

9 Answers 9

57

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.

2
  • 4
    Mostly 7 bit because the 8th bit was used for parity on many serial transmission protocols. Nov 5, 2009 at 19:31
  • 1
    You can't use binary strings in email because it might collide with the control characters. Jul 20, 2013 at 17:55
52

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.

5
  • 9
    And also, if You want to send binary data over string protocols (like JSON) where bianry could break Your string due to unwanted chars like quotes 0x00, etc...
    – user170442
    Nov 5, 2009 at 19:31
  • 2
    Rafal: That's basically an instance of a "restricted character set". Nov 5, 2009 at 19:32
  • 1
    I wish I could approve your answer too. Great answer.
    – Ismael
    Nov 5, 2009 at 19:51
  • 2
    If there is non Latin language like Hebrew or Arabic. Encoding the text to base64 produces ASCII characters only?
    – Costa
    Mar 1, 2015 at 13:52
  • 3
    @Costa encoding anything on base64 produces ASCII only. Base64 uses only A-Z a-z 0-9 and a combination of two of these: / + - _ : ! ~ usually: / +
    – Largato
    Dec 5, 2015 at 22:48
19
<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

7

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.

6

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.

2

Convert BLOB data to string and back...

0

Whether or not to use it depends on what you're using it for.

I've used it mostly for encoding binary data to pass through a mechanism that has really been created for text files. For example - when passing a digital certificate request around or retrieving the finished digital certificate -- in those cases, it's often very convenient to pass the binary data as Base 64 via a text field on a web form.

I probably wouldn't use it if you have something that is already text and you just want to pass it somewhere.

0

I use it for passing around files that tend to get chewed up by email programs because they look like text files (e.g. HL7 transcripts for replay).

0

Besides some of the advantages mentioned like compatibility and keeping the text string readible across platforms, languages and systems, there is one additional advantage that has not been mentioned on here. You can conceal the string from the customers' eyes.

For example, in our business we have a GET /product/<id-price-customer_id> route. Passing a string like /product/25-12.95-customer_293 that the customer could get a glimples of when an error occurs, it can be instead passed as an encoded string, such as, /product/MjQtMTQuOTktY3VzdG9tZXJfMjU2. While a base64 string is not secure per se and can be easily decrypted, it thus has a habit of preventing some scared customer enquiries who are always pondering about security.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.