Calculate actual data size from Base64 encoded string length

怎甘沉沦 提交于 2020-01-11 04:34:09

问题


Given data encoded as a Base64-encoded string, can I somehow calculate the actual length of the raw data that has been encoded only by looking at the length of the Base64-encoded string?

I don't want to traverse the string if not necessary (this also includes string operations on the trailling characters of the encoded string to check for padding).


回答1:


The exact length cannot be calculated unless you look at the padding. Without looking for padding, the best you can do is calculate an upper bound for the length by multiplying the encoded-string length with 3/4 (the encoded length is guaranteed to be exactly divisible by 4).

The upper bound calculated thus will be either N, N+1 or N+2, where N is the length of the raw data.



来源:https://stackoverflow.com/questions/6816137/calculate-actual-data-size-from-base64-encoded-string-length

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!