What is the size of the file if it is encoded in 450 KB base64 encoding?

Is it possible to calculate the size of the data if I know its size with base64 encoding?

I have a 450 KB file with base64 encoding, but what size is it unpacked?

Is there a way to find the size of the output without first unpacking the file?

+5
source share
2 answers

I have a 450 KB file with base64 encoding, but what size is it unpacked?

In fact, you are not β€œunpacking”, you are decoding. The result will be less than encoded data.

Since Base 64 encoding requires ~ 8 bits for each 6 bits of source data (or 4 bytes to store 3), the math is simple:

Encoded Decoded 450KB / 4 * 3 = ~ 337KB 

The overhead between Base64 and the decoded string is almost constant, 33.33%. I say "almost" just because padding bytes at the end ( = ), which make the string length a multiple of 4. See a few examples:

 String Encoded Len B64 Pad Space needed A QQ== 1 2 2 400.00% AB QUI= 2 3 1 200.00% ABC QUJD 3 4 0 133.33% ABCD QUJDRA== 4 6 2 200.00% ABCDEFGHIJKLMNOPQ QUJDREVGR0hJSktMTU5PUFE= 17 23 1 140.00% ( 300 bytes ) ( 400 bytes ) 300 400 0 133.33% ( 500 bytes ) ( 668 bytes ) 500 666 2 133.60% ( 5000 bytes ) ( 6668 bytes ) 5000 6666 2 133.36% ... tends to 133.33% ... 


Calculation of space for unregistered data:

Let me get the value QUJDREVGR0hJSktMTU5PUFE= mentioned above.

  • The encoded value has 24 bytes.

  • Let it be calculated 24/4 * 3 => the result is 18.

  • Let me count the number = at the end of the encoded value: In this case, 1 (we only need to check the last two bytes of the encoded data).

  • Having received 18 (obtained in step 2) - 1 (obtained in step 3), we obtain 17

So, we need 17 bytes to store the data.

+14
source

base64 adds about a third of its original size, so your file should be larger or smaller .75 * 450 KB.

+1
source

Source: https://habr.com/ru/post/1237472/


All Articles