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?
Something like:
BinaryStringSize is 64Kb EncodedBinaryStringSize will be 127Kb after encoding.
Oh, the code is in C.
Thanks.
If you do Base64 exactly right, and that includes padding the end with
=
characters, and you break it up with aCR LF
every 72 characters, the answer can be found with:In C, you may also terminate with a
\0
-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 tomalloc()
, you might actually prefer a version that wastes a few bytes, in order to make the coding simpler:geocar's answer was close, but could sometimes be off slightly.
There are 4 bytes output for every 3 bytes of input. If the input size is not a multiple of three, we must add to make it one. Otherwise leave it alone.
Divide this by 3, then multiply by 4. That is our total output size, including padding.
As I said in my comment, the total size must be divided by the line width before doubling to properly account for the last line. Otherwise the number of CRLF characters will be overestimated. I am also assuming there will only be a CRLF pair if the line is 72 characters. This includes the last line, but not if it is under 72 characters.
So put it all together:
Or to make it a bit more readable:
I ran into a similar situation in python, and using codecs.iterencode(text, "base64") the correct calculation was: