1

I'm trying to convert the base64 encoded string "CDA=" into a binary buffer, using JavaScript. I have tried calling the function atob, but the result is always an empty array.

I have tried atob with character strings, that I encoded with btoa, and atob provides the expected result. So it seems that it doesn't always fail, but probably only when the base64 string represent a binary data. From the internet, I see that binary data also should be managed... Does anyone have an explanation to this behaviour ?

Omar BELKHODJA
  • 1,622
  • 1
  • 12
  • 18

1 Answers1

1

atob() returns a string not an array.

Your Base64 string is 0x8 0x30 which is interpreted as <backspace><zero> when you look at it and see:

> window.atob("CDA=")
  "0"

However both bytes are present:

> window.atob("CDA=").charCodeAt(0)
  8

> window.atob("CDA=").charCodeAt(1)
  48

If you want an array, see Creating a Blob from a base64 string in JavaScript.

Community
  • 1
  • 1
Alex K.
  • 171,639
  • 30
  • 264
  • 288