Base64 转码

翠微 / 2024-01-10 / 原文

Base64 转码
btoa():任意值转为 Base64 编码
atob():Base64 编码转为原来的值

function b64Encode(str) {
  return btoa(encodeURIComponent(str));
}

function b64Decode(str) {
  return decodeURIComponent(atob(str));
}

b64Encode('你好') // "JUU0JUJEJUEwJUU1JUE1JUJE"
b64Decode('JUU0JUJEJUEwJUU1JUE1JUJE') // "你好"