90
votes

Why do we have Base64 encoding? I am a beginner and I really don't understand why would you obfuscate the bytes into something else (unless it is encryption). In one of the books I read Base64 encoding is useful when binary transmission is not possible. Eg. When we post a form it is encoded. But why do we convert bytes into letters? Couldn't we just convert bytes into string format with a space in between? For example, 00000001 00000004? Or simply 0000000100000004 without any space because bytes always come in pair of 8?

3
Because you just turned 1 byte into 8 rather than 3 into 4 as you do with base64 (if memory serves me correctly). Bandwidth is finite.Brian Roach
You're confusing bits (whit 8 bits you make a byte) and bytes, which can come in any kind of grouping.Denys Séguret

3 Answers

118
votes

Base64 is a way to encode binary data into an ASCII character set known to pretty much every computer system, in order to transmit the data without loss or modification of the contents itself. For example, mail systems cannot deal with binary data because they expect ASCII (textual) data. So if you want to transfer an image or another file, it will get corrupted because of the way it deals with the data.

Note: base64 encoding is NOT a way of encrypting, nor a way of compacting data. In fact a base64 encoded piece of data is 1.333… times bigger than the original datapiece. It is only a way to be sure that no data is lost or modified during the transfer.

15
votes

Base64 is a mechanism to enable representing and transferring binary data over mediums that allow only printable characters.It is most popular form of the “Base Encoding”, the others known in use being Base16 and Base32.

The need for Base64 arose from the need to attach binary content to emails like images, videos or arbitrary binary content . Since SMTP [RFC 5321] only allowed 7-bit US-ASCII characters within the messages, there was a need to represent these binary octet streams using the seven bit ASCII characters...

Hope this answers the Question

4
votes

Base64 is a more or less compact way of transmitting (encoding, in fact, but with goal of transmitting) any kind of binary data.

See http://en.wikipedia.org/wiki/Base64

"The general rule is to choose a set of 64 characters that is both part of a subset common to most encodings, and also printable."

That's a very general purpose and the common need is not to waste more space than needed.

Historically, it's based on the fact that there is a common subset of (almost) all encodings used to store chars into bytes and that a lot of the 2^8 possible bytes risk loss or transformations during simple data transfer (for example a copy-paste-emailsend-emailreceive-copy-paste sequence).

(please redirect upvote to Brian's comment, I just make it more complete and hopefully more clear).