Mike Chambers

code = joy

Base64 Encoding and Decoding in JavaScript in Adobe AIR

with 7 comments

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.

Written by mikechambers

June 12th, 2008 at 1:20 pm

Posted in General

7 Responses to 'Base64 Encoding and Decoding in JavaScript in Adobe AIR'

Subscribe to comments with RSS or TrackBack to 'Base64 Encoding and Decoding in JavaScript in Adobe AIR'.

  1. 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

  2. Yes. In general, the webkit docs are poor.

    mike chambers

    mesh@adobe.com

    mikechambers

    12 Jun 08 at 9:27 pm

  3. [...] 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) [...]

  4. what about speed flash demo is: http://www.pikniktube.com ? thanks.

    ib0

    16 Jun 08 at 12:47 am

  5. 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

  6. @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

  7. [...] Base64 Encoding and Decoding in JavaScript in Adobe AIR [...]

Leave a Reply