JavaScript 没有正确编码和解码字符

JavaScript not correctly encoding and decoding the characters

我的以下代码解码不正确。

btoa(atob("nirajan")) //Output: "nirajak="

但是下面这段代码工作得很好

btoa(atob("niranjan")) //Output: "niranjan"

谁能解释一下第一部分有什么问题以及两个代码示例之间的区别。

PS:在 Chrome

上测试

btoa() encodes the string and atob() 解码字符串。

所以你的方法倒退了。首先,对内部函数使用 btoa 进行编码,然后对外部函数使用 atob 进行解码。

atob(btoa('Hello world!')); // returns 'Hello World!'