views:

473

answers:

3

Hi

just wondering if there are any methods in JavaScript that i can use to encode and decode a string using base64 encoding?

thank you

+3  A: 

I don't think that there are pre-written functions to handle Base64 but there are plenty of resources available online! Here's one http://ntt.cc/2008/01/19/base64-encoder-decoder-with-javascript.html

Ranhiru Cooray
+1  A: 

The php.js project has JavaScript implementations of many of PHP's functions. base64_encode and base64_decode are included.

ceejayoz
A: 

Short and fast Base64 Javascript Decode Funtion without Failsafe:

    function decode_base64(s) {
    var e={},i,k,v=[],r='',w=String.fromCharCode;
    var n=[[65,91],[97,123],[48,58],[47,48],[43,44]];

    for(z in n){for(i=n[z][0];i<n[z][1];i++){v.push(w(i));}}
    for(i=0;i<64;i++){e[v[i]]=i;}

    for(i=0;i<s.length;i+=72){
    var b=0,c,x,l=0,o=s.substring(i,i+72);
         for(x=0;x<o.length;x++){
                c=e[o.charAt(x)];b=(b<<6)+c;l+=6;
                while(l>=8){r+=w((b>>>(l-=8))%256);}
         }
    }
    return r;
    }

Regards

         ..Sniper
Sniper