If a server received a base64 string and wanted to check it's length before converting,, say it wanted to always permit the final byte array to be 16KB. How big could a 16KB byte array possibly become when converted to a Base64 string (assuming one byte per character)?
6 Answers
Base64 encodes each set of three bytes into four bytes. In addition the output is padded to always be a multiple of four.
This means that the size of the base-64 representation of a string of size n is:
ceil(n / 3) * 4
So, for a 16kB array, the base-64 representation will be ceil(16*1024/3)*4 = 21848 bytes long ~= 21.8kB.
A rough approximation would be that the size of the data is increased to 4/3 of the original.
-
-
@vIceBerg, It depends on whether you are using
ceil
withfloat
numbers, or justint
numbers. (and noceil
) Jul 26, 2016 at 12:57 -
10I guess the simpler way to put this is that you add 1/3 of original size.– mvmnOct 31, 2016 at 11:39
-
2In the example you proposed, showing the result in the same order of measure would increase a bit the quality of the answer (21,3 KB instead of 21848 Bytes). Feb 3, 2017 at 12:23
-
Be careful with units, kB (1000 B) aren't KiB (1024 B), so it is ceil(16*1000/3)*4 bytes see en.wikipedia.org/wiki/Binary_prefix Nov 16 at 10:04
From Wikipedia
Note that given an input of n bytes, the output will be (n + 2 - ((n + 2) % 3)) / 3 * 4 bytes long, so that the number of output bytes per input byte converges to 4 / 3 or 1.33333 for large n.
So 16kb * 4 / 3 gives a little over 21.8 kb, or 21,848 bytes to be exact.
Hope this helps
-
-
@AlexMisiulia kB is for kilobyte, which is 1000 bytes. 1024 bytes is a kibibyte or KiB.– MackNov 21 at 2:26
-
@mack I have never seen kibibyte or KiB used outside of a scientific paper, and it is commonly understood that there are 1024 bytes in a kilobyte, even if that understanding is technically incorrect. Nov 21 at 10:15
16kb is 131,072 bits. Base64 packs 24-bit buffers into four 6-bit characters apiece, so you would have 5,462 * 4 = 21,848 bytes.
-
16kB is 128,000 bits, not 131,072 bits (or 16 kiB, see en.wikipedia.org/wiki/Binary_prefix) Nov 16 at 10:09
Since the question was about the worst possible increase, I must add that there are usually line breaks at around each 80 characters. This means that if you are saving base64 encoded data into a text file on Windows it will add 2 bytes, on Linux 1 byte for each line.
The increase from the actual encoding has been described above.
-
9Isn't the extreme case that 1 source byte becomes 4 base64 bytes, so a 4x increase? Any longer source material gets a better ratio until, as others have said, it asymptotically approaches 1.333...– OlieMay 4, 2016 at 22:02
This is a future reference for myself. Since the question is on worst case, we should take line breaks into account. While RFC 1421 defines maximum line length to be 64 char, RFC 2045 (MIME) states there'd be 76 char in one line at most.
The latter is what C# library has implemented. So in Windows environment where a line break is 2 chars (\r\n), we get this: Length = Floor(Ceiling(N/3) * 4 * 78 / 76)
Note: Flooring is because during my test with C#, if the last line ends at exactly 76 chars, no line-break follows.
I can prove it by running the following code:
byte[] bytes = new byte[16 * 1024];
Console.WriteLine(Convert.ToBase64String(bytes, Base64FormattingOptions.InsertLineBreaks).Length);
The answer for 16 kBytes encoded to base64 with 76-char lines: 22422 chars
Assume in Linux it'd be Length = Floor(Ceiling(N/3) * 4 * 77 / 76)
but I didn't get around to test it on my .NET core yet.
Also it would depend on actual character encoding, i.e. if we encode to UTF-32 string, each base64 character would consume 3 additional bytes (4 byte per char).