btoa ( binary to ascii ) 和 atob ( ascii to binary ) 是 window的两个对象, 用于将字符串转为 base64 编码以及将 base64 编码解密为 字符串
// Define the string
var string = 'hello world!'
// Encode the string
var encodeString = btoa(string)
console.log(encodeString) // "aGVsbG8gd29ybGQh"
// Decode the String
var decodeString = atob(encodeString)
console.log(decodeString) // 'hello world!'
汉字不行, 至于为什么, 应该跟这个有关系:
Note: 由于这个函数将每个字符视为二进制数据的字节,而不管实际组成字符的字节数是多少,
所以如果任何字符的码位超出 0x00 ~ 0xFF 范围,则会引发 InvalidCharacterError 异常
。请参阅 Unicode_字符串 ,该示例演示如何编码字符数超出 0x00 ~ 0xFF 范围的字符串
以上~