NiRUS
NiRUS

Reputation: 4269

JavaScript not correctly encoding and decoding the characters

I have the below code that is decoding incorrectly.

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

but this below code works perfectly

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

Can somebody explain what is wrong with the first part and difference between the two code examples.

PS: Tested on Chrome

Upvotes: 2

Views: 783

Answers (1)

romellem
romellem

Reputation: 6503

btoa() encodes the string and atob() decodes the string.

So you have your methods backwards. First, encode with btoa with the inner function, then, decode with atob on the outer function.

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

Upvotes: 2

Related Questions