optimization - Why padding is used in Base64 encoding? -
possible duplicate:
why base64 encoding requires padding if input length not divisible 3?
quoting wikipedia:
...these padding characters must discarded when decoding still allow calculation of effective length of unencoded text, when input binary length not multiple of 3 bytes. ...
but calculation of length raw data can done if strip 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 . . .
so given real encoded size (third column) can correctly guess padded size be:
paddedsize = 4 * ceil (realsize / 4)
so in theory, there no need of padding. algorithm have handled it. considering base64 encoding popular industry standard, used in many applications , devices. these have benefited reduced encoded size. question is, why padding used in base64 encoding?
it makes encoded message integer multiple of 4 characters. might make writing decoder easier. can load , process characters in blocks of 4 , convert them 3 output characters, , padding makes easy without going off end of string.
Comments
Post a Comment