最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What is a base64 length in bytes? - Stack Overflow

programmeradmin3浏览0评论

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
  • You mean the length of the file after you decode it from base64? You have to decode it first. – Robert Harvey Commented Jul 1, 2013 at 16:41
  • Base64 - 5 bits, Normal chars - 8 bits => size * 1.6 – mishik Commented Jul 1, 2013 at 16:43
  • Take a look at: stackoverflow.com/questions/1533113/… and (stackoverflow.com/questions/4715415/…) (you will need to reverse those formulas) – Anthony Accioly Commented Jul 1, 2013 at 16:48
Add a comment  | 

3 Answers 3

Reset to default 10

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

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;
发布评论

评论列表(0)

  1. 暂无评论