根据 Base64 编码的字符串长度计算实际数据大小 [英] Calculate actual data size from Base64 encoded string length

查看:58
本文介绍了根据 Base64 编码的字符串长度计算实际数据大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定编码为 Base64 编码字符串的数据,我是否可以通过查看 Base64 编码字符串的长度以某种方式计算已编码的原始数据的实际长度?p>

如果没有必要,我不想遍历字符串(这还包括对编码字符串的尾随字符进行字符串操作以检查填充).

解决方案

确切的长度无法计算,除非您查看填充.在不寻找填充的情况下,您可以做的最好的事情是通过将编码字符串长度乘以 3/4 来计算长度的上限(保证编码长度可以被 4 整除).

因此计算的上限将是 NN+1N+2,其中 N 是原始数据的长度.

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).

解决方案

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.

这篇关于根据 Base64 编码的字符串长度计算实际数据大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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