在 JavaScript 中,有两个函数被分别用来处理解码和编码 base64 字符串:
- atob() //ASCII to Base64
- btoa() //Base64 to ASCII
atob() 函数能够解码通过base-64编码的字符串数据。相反地,btoa() 函数能够从二进制数据“字符串”创建一个base-64编码的ASCII字符串。
Encoded size increase(编码大小增长)
Each Base64 digit represents exactly 6 bits of data. So, three 8-bits bytes of the input string/binary file (3×8 bits = 24 bits) can be represented by four 6-bit Base64 digits (4×6 = 24 bits).(每一个base64数位明确表示6位数据,所以3个8位的字符或者二进制输入能够被4个6位的base64位数代表,4×6 = 3×8)
This means that the Base64 version of a string or file will be at most 133% the size of its source (a ~33% increase). The increase may be larger if the encoded data is small. For example, the string “a” with length === 1 gets encoded to “YQ==” with length === 4 — a 300% increase.(这代表着Base64编码的字符串或者文件的大小最大是源的133%倍,如果被编码的数据非常的小,那么这个比例将会非常的大,比如字符串"a" 的长度是1,但是编码后是4个长度的"YQ==")
基本用法
//更多内容:
https://developer.mozilla.org/zh-CN/docs/Web/API/WindowBase64/Base64_encoding_and_decoding
https://developer.mozilla.org/zh-CN/docs/Web/API/WindowBase64/atob