mike chambers | about

Base64 Encoding and Decoding in JavaScript in Adobe AIR

I have been working on a JavaScript AIR app for the past week or so while on the on AIR Tour in Europe, and ran into the need to Base64 encode some data. I knew that there were some JavaScript libraries that did this, and I could also create a SWF library that used the Flex Base64Encoder class. However, I didn’t like either of these solutions as I was concerned about performance with using a JavaScript implementation, and didn’t want to hassle with creating a SWF library to link into JavaScript.

Well, after some searching on Google, I discovered that webkit has native Base64 encoding and decoding functions, respectively named btoa and atob.

Here is how you can use them from JavaScript:

var s = "foo";
var encoded = btoa(s);
var decoded = atob(encoded);

This outputs:


Among other things, this can come in handy if you need to create custom HTTP authentication headers.

You can find more information on the apis here.

twitter github flickr behance