计算 Base 64 编码消息的大小 [英] Calculate the size to a Base 64 encoded message
本文介绍了计算 Base 64 编码消息的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个二进制字符串,我用 Base 64 编码.现在,我需要事先知道最终 Base 64 编码字符串的大小.
I have a binary string that I am encoding in Base 64. Now, I need to know before hand the size of the final Base 64 encoded string will be.
有什么方法可以计算吗?
Is there any way to calculate that?
类似:
BinaryStringSize 为 64Kb编码后 EncodedBinaryStringSize 为 127Kb.
BinaryStringSize is 64Kb EncodedBinaryStringSize will be 127Kb after encoding.
哦,代码是 C 语言的.
Oh, the code is in C.
谢谢.
推荐答案
如果你做Base64 完全正确正确,其中包括用 =
字符填充结尾,然后每 72 个字符用 CR LF
将其分解,答案可以通过以下方式找到:
If you do Base64 exactly right, and that includes padding the end with =
characters, and you break it up with a CR LF
every 72 characters, the answer can be found with:
code_size = ((input_size * 4) / 3);
padding_size = (input_size % 3) ? (3 - (input_size % 3)) : 0;
crlfs_size = 2 + (2 * (code_size + padding_size) / 72);
total_size = code_size + padding_size + crlfs_size;
在 C 中,您也可以使用