Base64编码长度计算?
的阅读的base64 维基后 ... 的
After reading the base64 wiki ...
我想弄清楚的怎么样的公式工作:
I'm trying to figure out how's the formula working :
由于与 N
,以base64长度将
Given a string with length of n
, the base64 length will be
这就是: 4 * Math.Ceiling(((双)s.Length / 3)))
我已经知道的base64长度必须%4 == 0
来让去codeR知道什么是原来的文本长度。
I already know that base64 length must be %4==0
to allow the decoder know what was the original text length.
填充的序列中的最大数量可 =
或 ==
。
The max number of padding for a sequence can be =
or ==
.
维基:每个输入字节输出的字节数大约为4/3(33%
开销)
wiki :The number of output bytes per input byte is approximately 4 / 3 (33% overhead)
问:
如何请问以上信息与输出长度定居 ?
How does the information above settle with the output length ?
每个字符被用来重新present 6位( LOG2(64)= 6
) 。
Each character is used to represent 6 bits (log2(64) = 6
).
因此4个字符用来重新present 4 * 6 = 24位= 3字节
。
Therefore 4 chars are used to represent 4 * 6 = 24 bits = 3 bytes
.
所以,你需要 4 *(N / 3)
字符重新present N
字节,这需要四舍五入为4的倍数
So you need 4*(n/3)
chars to represent n
bytes, and this needs to be rounded up to a multiple of 4.
这向上舍入至4的倍数所得未使用的填充字符的数量将明显是0,1,2或3个
The number of unused padding chars resulting from the rounding up to a multiple of 4 will obviously be 0, 1, 2 or 3.