维基百科上说

当需要对二进制数据进行编码时,通常使用Base64编码方案,这些数据需要通过设计用于处理文本数据的媒体存储和传输。这是为了确保数据在传输过程中保持完整而不被修改。

但是,数据不总是以二进制存储/传输吗?因为我们机器的内存存储二进制,这只是取决于你如何解释它?因此,无论您将位模式010011010110000101101110编码为ASCII中的Man还是Base64中的TWFu,最终都将存储相同的位模式。

如果最终的编码是用0和1表示的,并且每台机器和媒体都可以处理它们,那么数据是用ASCII还是Base64表示有什么关系呢?

“设计用于处理文本数据的媒体”是什么意思?他们可以处理二进制=>他们可以处理任何东西。


谢谢大家,我想我现在明白了。

当我们发送数据时,我们不能确定数据将以与我们预期的相同格式进行解释。因此,我们发送以双方都能理解的某种格式(如Base64)编码的数据。这样,即使发送方和接收方对相同内容的解释不同,但由于他们对编码格式达成一致,数据也不会被错误地解释。

来自Mark Byers的例子

如果我想发送

Hello
world!

一种方法是用ASCII码发送

72 101 108 108 111 10 119 111 114 108 100 33

但是字节10在另一端可能不能被正确地解释为换行符。因此,我们使用ASCII的一个子集来像这样编码

83 71 86 115 98 71 56 115 67 110 100 118 99 109 120 107 73 61 61

以传输相同数量信息的更多数据为代价,确保接收者能够以预期的方式解码数据,即使接收者对其余字符集有不同的解释。


当前回答

为文本数据设计的媒体当然最终也是二进制的,但是文本媒体通常使用某些二进制值作为控制字符。此外,文本媒体可能会拒绝某些二进制值作为非文本。

Base64编码将二进制数据编码为只能在文本媒体中解释为文本的值,并且不包含任何特殊字符和/或控制字符,因此数据也可以在文本媒体中保存。

其他回答

大多数计算机以8位二进制格式存储数据,但这不是必需的。一些机器和传输介质一次只能处理7位(甚至更少)。这样的介质将以7位的倍数来解释流,因此如果您要发送8位的数据,您将不会在另一端收到您所期望的内容。Base-64只是解决这个问题的一种方法:将输入编码为6位格式,通过媒体发送,然后在接收端解码回8位格式。

为什么/如何使用Base64编码?

Base64 is one of the binary-to-text encoding scheme having 75% efficiency. It is used so that typical binary data (such as images) may be safely sent over legacy "not 8-bit clean" channels. In earlier email networks (till early 1990s), most email messages were plain text in the 7-bit US-ASCII character set. So many early comm protocol standards were designed to work over "7-bit" comm links "not 8-bit clean". Scheme efficiency is the ratio between number of bits in the input and the number of bits in the encoded output. Hexadecimal (Base16) is also one of the binary-to-text encoding scheme with 50% efficiency.

Base64编码步骤(简化):

Binary data is arranged in continuous chunks of 24 bits (3 bytes) each. Each 24 bits chunk is grouped in to four parts of 6 bit each. Each 6 bit group is converted into their corresponding Base64 character values, i.e. Base64 encoding converts three octets into four encoded characters. The ratio of output bytes to input bytes is 4:3 (33% overhead). Interestingly, the same characters will be encoded differently depending on their position within the three-octet group which is encoded to produce the four characters. The receiver will have to reverse this process to recover the original message.

为什么不看看当前定义Base64的RFC呢?

Base encoding of data is used in many situations to store or transfer data in environments that, perhaps for legacy reasons, are restricted to US-ASCII [1] data.Base encoding can also be used in new applications that do not have legacy restrictions, simply because it makes it possible to manipulate objects with text editors. In the past, different applications have had different requirements and thus sometimes implemented base encodings in slightly different ways. Today, protocol specifications sometimes use base encodings in general, and "base64" in particular, without a precise description or reference. Multipurpose Internet Mail Extensions (MIME) [4] is often used as a reference for base64 without considering the consequences for line-wrapping or non-alphabet characters. The purpose of this specification is to establish common alphabet and encoding considerations. This will hopefully reduce ambiguity in other documents, leading to better interoperability.

Base64最初被设计为一种允许二进制数据附加到电子邮件的方式,作为多用途互联网邮件扩展的一部分。

除了其他(有点冗长)的答案:即使忽略只支持7位ASCII的旧系统,在文本模式下提供二进制数据的基本问题是:

换行符通常以文本模式进行转换。 必须注意不要将NUL字节作为文本字符串的结尾,这在任何带有C血统的程序中都很容易做到。

为文本数据设计的媒体当然最终也是二进制的,但是文本媒体通常使用某些二进制值作为控制字符。此外,文本媒体可能会拒绝某些二进制值作为非文本。

Base64编码将二进制数据编码为只能在文本媒体中解释为文本的值,并且不包含任何特殊字符和/或控制字符,因此数据也可以在文本媒体中保存。