0

I noticed that when I convert a javascript string to base64 with atob and then back to text with btoa it returns a string different than where I started. I am wondering why?

Example:

btoa(atob('hello world'));

The result of this in Chrome dev tools is: "helloworlQ=="

Any ideas why?

Community
  • 1
  • 1
Kevin192291
  • 1,925
  • 4
  • 26
  • 43

2 Answers2

1

You are using the functions the wrong way around. "hello world" is a valid base64 string, but what you want is atob(btoa())

lazyguy
  • 107
  • 6
1

To convert ASCII to Base64, you need to do

btoa("hello world"); // "aGVsbG8gd29ybGQ="

To convert Base64 to ASCII, you do

atob("aGVsbG8gd29ybGQ="); // "hello world"

The names of the atob and btoa functions are very confusing.

Adrian Theodorescu
  • 11,664
  • 2
  • 23
  • 30