Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a base64 length in bytes?

I have a file encoded in Base64 with a certain file length. How do I get the size in bytes?

Example:

var size= canvas.toDataURL();   

console.log(resizeCanvasURL.length);


// -> 132787  base64 length
like image 375
daisy Avatar asked Jul 01 '13 16:07

daisy


2 Answers

Each symbol in Base64 encoding holds 6 bits of information. Each symbol in normal file hold 8 bits. Since after decoding you have the same amount of information you need:

normal file size - 1000 bytes
base64 file size - 1333 bytes
like image 139
mishik Avatar answered Oct 17 '22 11:10

mishik


The answer from mishik is wrong.

Base64 is filled up with =-chars (to have the right amount of bits). So it should be

    var byteLength = parseInt((str).replace(/=/g,"").length * 0.75));
like image 36
Vincent Hoch-Drei Avatar answered Oct 17 '22 13:10

Vincent Hoch-Drei