JavaScript字符串和Base64互转
9374 2022/12/27 jsbase64
JavaScript提供了两个原生Base64相关方法:
btoa():字符串或二进制值转为Base64编码
atob():Base64编码转为原来的编码
function encode () {
let str = encodeURI("哈哈");
let base64 = btoa(str );
console.log(base64);
}
1
2
3
4
5
2
3
4
5
function decode () {
let haha = atob("哈哈");
let str = decodeURI(haha);
console.log(str);
1
2
3
4
5
6
2
3
4
5
6