我知道什么是base64编码,以及如何在c#中计算base64编码,但是我已经看到过几次,当我将一个字符串转换为base64时,在结尾有一个=。
他提出了几个问题:
base64字符串总是以=结尾吗? 为什么an =要加在后面?
我知道什么是base64编码,以及如何在c#中计算base64编码,但是我已经看到过几次,当我将一个字符串转换为base64时,在结尾有一个=。
他提出了几个问题:
base64字符串总是以=结尾吗? 为什么an =要加在后面?
当前回答
等号(=)在某些形式的base64编码中用作填充。关于base64的维基百科文章有所有的细节。
其他回答
等号(=)在某些形式的base64编码中用作填充。关于base64的维基百科文章有所有的细节。
http://www.hcidata.info/base64.htm
将“Mary had”编码为64进制
In this example we are using a simple text string ("Mary had") but the principle holds no matter what the data is (e.g. graphics file). To convert each 24 bits of input data to 32 bits of output, Base 64 encoding splits the 24 bits into 4 chunks of 6 bits. The first problem we notice is that "Mary had" is not a multiple of 3 bytes - it is 8 bytes long. Because of this, the last group of bits is only 4 bits long. To remedy this we add two extra bits of '0' and remember this fact by putting a '=' at the end. If the text string to be converted to Base 64 was 7 bytes long, the last group would have had 2 bits. In this case we would have added four extra bits of '0' and remember this fact by putting '==' at the end.
从维基百科:
最后的'=='序列表示最后一组只包含一个字节,'='表示它包含两个字节。
因此,这是某种填充。
这是填充。从http://en.wikipedia.org/wiki/Base64:
理论上,解码不需要填充字符,因为 缺失的字节数可以从Base64的数目计算出来 位数。在某些实现中,填充字符是强制的, 而对另一些人则不使用。填充字符的一种情况 需要连接多个Base64编码的文件。
它起到填充的作用。
一个更完整的答案是,base64编码的字符串并不总是以一个=结尾,如果需要将字符串填充到适当的长度,它只会以一个或两个=结尾。