1

I have a 200Kb jpg and I want to convert it into a base64 string.

How long will that base64 string be approximately ?

The reason I'm asking is because I'd like to store that image as a base64 string in a "container" that only allows strings of a maximum length is 65000 characters.

I tried to find out for myself using the Chrome's console but the browser keeps freezing up due to the length of the base64 generated string, as soon as I assign it to a variable and the do :

x = 'base64.....'; // ridiculously long string
x.length;
Kawd
  • 4,122
  • 10
  • 37
  • 68
  • 200kb is 200000 "characters", more than 3 times longer than 65000. Common sense should tell that the base64 representation can't possibly be 3 times smaller than the original file. Othewise it would be by far the world's best compression algorithm and we'd use it for everything. – JJJ Apr 01 '16 at 09:09
  • 1
    base64-encoding is approximately 4/3 the original size, due to how the bits are packed. So a 200Kb jpg will be about 266Kb in its base64-encoded form. – Niet the Dark Absol Apr 01 '16 at 09:11
  • 1
    Read this http://stackoverflow.com/questions/4715415/base64-what-is-the-worst-possible-increase-in-space-usage and this http://stackoverflow.com/questions/11402329/base64-encoded-image-size – Lewis Apr 01 '16 at 09:20

2 Answers2

4

The approximate size of the string is 135% of the original size due to the expansion that takes place (according to my NetBSD manpage of uuencode). To encode n bytes you need 4*ceil(n/3) bytes and additional line breaks.

149
  • 45
  • 4
1

As already stated in comments, there's no way of giving an exact number as it's depending on how the data is packed. It will however be larger than the source file. A ballpark figure is around 270 000 characters.

An easy way to check this is to upload a few images to an online converting service such as https://www.base64-image.de/

Emil Oberg
  • 4,006
  • 18
  • 30
  • Perfect, That's all I needed to know Emil! I was only interested in the ratio. Its funny how a comment got the concept of common sense mixed up with the concept of acquired knowledge. – Kawd Apr 01 '16 at 10:01