为什么填充在Base64编码中使用? [英] Why padding is used in Base64 encoding?

查看:288
本文介绍了为什么填充在Base64编码中使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/4080988/why-does-base64-encoding-requires-padding-if-the-input-length-is-not-divisible-by\">Why确实base64编码需要填充,如果输入长度不是被3整除?

......这些填充字符必须再
  解码但仍时被丢弃
  允许有效的计算
  在unen codeD文本的长度,当其
  输入的二进制长度将不会是一个
  的3个字节的倍数。 ...

...these padding characters must then be discarded when decoding but still allow the calculation of the effective length of the unencoded text, when its input binary length would not be a multiple of 3 bytes. ...

但是,即使剥去填充字符长度的原始数据的计算可以容易地进行。

But the calculation of length raw data can easily be done even if strip the padding character.

          |               Encoded
          |--------------------------------------
Raw Size  | Total Size | Real Size | Padding Size
1         | 4          | 2         | 2
2         | 4          | 3         | 1
3         | 4          | 4         | 0
4         | 8          | 6         | 2
5         | 8          | 7         | 1
6         | 8          | 8         | 0
7         | 12         | 10        | 2
8         | 12         | 11        | 1
9         | 12         | 12        | 0
10        | 16         | 14        | 2
.
.
.

所以,有了真正的EN codeD大小(第三列),你总是可以正确地猜出填充的大小将是:

So given the real encoded size (third column) you can always correctly guess what padded size would be:

PaddedSize = 4 * Ceil (RealSize / 4)

因此​​,在理论上,没有必要填充。算法会处理它。考虑到Base64编码是一种流行的工业标准,它在许多应用程序和设备中使用。这些将通过减少连接codeD大小受益。所以问题是,为什么填充在Base64编码中使用?

So in theory, there was no need of padding. Algorithm would have handled it. Considering that Base64 encoding is a popular industry standard, it is used in many applications and devices. These would have benefited from reduced encoded size. So question is, why padding is used in Base64 encoding?

推荐答案

这使得连接codeD消息的4个字符的整数倍。这可能使编写一个去codeR稍微容易些。您可以载入并在4块过程字符,并将其转换为3个输出字符,填充可以很容易地做到这一点没有打算关闭字符串的结尾。

It makes the encoded message an integer multiple of 4 characters. This might make writing a decoder slightly easier. You can load and process characters in blocks of 4 and convert them to 3 output characters, and the padding makes it easy to do this without going off the end of the string.

这篇关于为什么填充在Base64编码中使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆