计算 Base 64 编码消息的大小 [英] Calculate the size to a Base 64 encoded message

查看:13
本文介绍了计算 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 中,您也可以使用 -byte 终止,因此那里会有一个额外的字节,并且您可能希望在每个代码的末尾进行长度检查编写它们,因此如果您只是在寻找传递给 malloc() 的内容,您实际上可能更喜欢浪费几个字节的版本,以使编码更简单:

In C, you may also terminate with a -byte, so there'll be an extra byte there, and you may want to length-check at the end of every code as you write them, so if you're just looking for what you pass to malloc(), you might actually prefer a version that wastes a few bytes, in order to make the coding simpler:

output_size = ((input_size * 4) / 3) + (input_size / 96) + 6;

这篇关于计算 Base 64 编码消息的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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