var base64 = '5oiR';
var dbase64 = decodeURIComponent(escape(window.atob(base64)));
console.log(dbase64);//我
现在解码中文base64是使用的这种方法,但是escape将来会从标准中删除,请问有什么替代escape的函数没有呢?试了一下encodeURI和encodeURIComponent都不行。(不考虑第三方库)
2023-2-24补充
也可以使用这种办法解决encodeURIComponent('我')
编码'我'字可以得到'%E6%88%91'
,'我'字的bease64编码是'5oiR'
- 使用
atob
函数对'我'字的base64解码会得到'æ\x88\x91'
使用split
对'æ\x88\x91'
进行按码元拆分得到码元构成的数组['æ', '\x88', '\x91']
- 对每一个码元进行处理在首部增加
%
后方拼接上当前码元的UTF-16代码单元转为16进制后的值后会得到这样的一个数组['%e6', '%88', '%91']
使用
join('')
把所有码元拼接起来会得到'%e6%88%91'
,现在就已经得到之前提到的encodeURIComponent('我')
编码'我'字可以得到'%E6%88%91'
- 使用
decodeURIComponent
解码
var base64 = '5oiR';
decodeURIComponent(
atob(base64).split('').map((charCode)=>
'%' +charCode.charCodeAt(0).toString(16)
).join('')
)
https://stackoverflow.com/que...