javascript 什么是 base64 长度(以字节为单位)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17409496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 08:13:29  来源:igfitidea点击:

What is a base64 length in bytes?

javascriptcanvasbase64

提问by daisy

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

我有一个以 Base64 编码的文件,具有一定的文件长度。我如何获得以字节为单位的大小?

Example:

例子:

var size= canvas.toDataURL();   

console.log(resizeCanvasURL.length);


// -> 132787  base64 length

回答by mishik

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:

Base64 编码中的每个符号包含 6 位信息。普通文件中的每个符号占 8 位。由于解码后您拥有所需的相同数量的信息:

normal file size - 1000 bytes
base64 file size - 1333 bytes

回答by Vincent Hoch-Drei

The answer from mishik is wrong.

mishik 的回答是错误的。

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

Base64 用 =-chars 填充(具有正确数量的位)。所以应该是

    var byteLength = parseInt((str).replace(/=/g,"").length * 0.75));