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
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
Share
Improve this question
edited Jul 1, 2013 at 16:45
daisy
asked Jul 1, 2013 at 16:39
daisydaisy
6753 gold badges8 silver badges17 bronze badges
3
|
3 Answers
Reset to default 10Each 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
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));
const stringLength = resizeCanvasURL.length
const sizeInBytes = 4 * Math.ceil(stringLength / 3) * 0.5624896334383812;
const sizeInKb = sizeInBytes / 1000;
OR
const stringLength = resizeCanvasURL.length
const sizeInBytes = stringLength * (3 / 4) - 2;
const sizeInKb = sizeInBytesNew / 1000;
size * 1.6
– mishik Commented Jul 1, 2013 at 16:43