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 didnt like either of these solutions as I was concerned about performance with using a JavaScript implementation, and didnt 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);
window.runtime.trace(s);
window.runtime.trace(encoded);
window.runtime.trace(decoded);
This outputs:
foo Zm9v foo
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.







Interesting! I wish stuff like this wasn’t so hard to find, though. WebKit sometimes seems to suffer from a lack of very clear documentation — the webkit-specific CSS rules come to mind.
Edward Finkler
12 Jun 08 at 4:58 pm
Yes. In general, the webkit docs are poor.
mike chambers
mesh@adobe.com
mikechambers
12 Jun 08 at 9:27 pm
[...] Mike Chambers » Blog Archive » Base64 Encoding and Decoding in JavaScript in Adobe AIR Why do I have a feeling I’ll need this eventually.. (tags: adobe encoding air javascript BASE64) [...]
links for 2008-06-15 | iKeif
15 Jun 08 at 9:31 am
what about speed flash demo is: http://www.pikniktube.com ? thanks.
ib0
16 Jun 08 at 12:47 am
Encoding and decoding Base64 is not difficult. Check out http://rumkin.com/tools/cipher/base64.php. Tyler Akins’ routines work in non-WebKit browsers.
B. Wilson
18 Jun 08 at 8:26 am
@B.Wilson - but the point is that it will work natively in AIR without needing an additional module. Handy link though, thanks.
Odd to see such poor webkit docs when it’s probably one of the more licensed/used of the engines… Safari, MobileSafari, AIR to name some. Samsung is using it as their embedded browser as well.
Victor
25 Jun 08 at 7:05 am
[...] Base64 Encoding and Decoding in JavaScript in Adobe AIR [...]
blogging and scraping » $ PHP decode from and encode to Base64 $
20 Oct 08 at 7:24 pm