维基百科上说
当需要对二进制数据进行编码时,通常使用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编码?
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.
而不是转义特殊字符
我将给您一个非常不同但真实的例子:我编写javascript代码以在浏览器中运行。HTML标记有ID值,但是在ID中哪些字符有效是有限制的。
但是我希望我的ID无损地引用文件系统中的文件。现实中的文件中可以有各种各样奇怪而奇妙的字符,从感叹号、重音字符、波浪号,甚至是表情符号!我不能这样做:
<div id="/path/to/my_strangely_named_file!@().jpg">
<img src="http://myserver.com/path/to/my_strangely_named_file!@().jpg">
Here's a pic I took in Moscow.
</div>
假设我想运行这样的代码:
# ERROR
document.getElementById("/path/to/my_strangely_named_file!@().jpg");
我认为这段代码在执行时会失败。
使用Base64,我可以引用一些复杂的东西,而不用担心哪种语言允许哪些特殊字符以及哪些需要转义:
document.getElementById("18GerPD8fY4iTbNpC9hHNXNHyrDMampPLA");
与使用MD5或其他哈希函数不同,您可以反向编码以找出真正有用的数据。
我希望我64年前就知道Base64。如果使用' encodeURIComponent '和str.replace(' \n ', ' \\n '),我就不会把我的头发扯掉了。
SSH传输文本:
如果你试图通过ssh传递复杂的数据(例如,一个dotfile,这样你就可以得到你的shell个性化),在没有base64的情况下,祝你好运。这是你用64进制来做的(我知道你可以使用SCP,但这需要多个命令-这使得ssh到服务器的键绑定复杂化):
https://superuser.com/a/1376076/114723