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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ismael
  • 2,330
  • 1
  • 25
  • 37
  • 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). – Jonathan Leffler Nov 05 '09 at 20:24
  • 6
    For disadvantages, look here http://stackoverflow.com/questions/14418785 – Val Jan 19 '13 at 21:13
  • Make use of this link for clear understanding. http://stackoverflow.com/a/201510/3979414 – Kumar Feb 17 '16 at 13:02

8 Answers8

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.

Adam Davis
  • 91,931
  • 60
  • 264
  • 330
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.

Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
  • 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... –  Nov 05 '09 at 19:31
  • 2
    Rafal: That's basically an instance of a "restricted character set". – Mehrdad Afshari Nov 05 '09 at 19:32
  • 1
    I wish I could approve your answer too. Great answer. – Ismael Nov 05 '09 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 01 '15 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: `/` `+` – Berin do CD Dec 05 '15 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

bluish
  • 26,356
  • 27
  • 122
  • 180
Foks
  • 191
  • 1
  • 2
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Marc DiMillo
  • 513
  • 5
  • 17
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
2

Convert BLOB data to string and back...

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
zudokod
  • 4,074
  • 2
  • 21
  • 24
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.

bethlakshmi
  • 4,581
  • 22
  • 44
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).

Joshua
  • 40,822
  • 8
  • 72
  • 132