Best Text Base For Javascript Mac

Active1 month ago

RJ TextEd is a full featured text and source editor with Unicode support. It is also a very powerful web (PHP, ASP, JavaScript, HTML and CSS) development editor. The functionality extends beyond text files and includes support for CSS/HTML editing with integrated CSS/HTML preview, spell checking, auto completion, HTML validation, templates and more.

Are there any methods in JavaScript that could be used to encode and decode a string using base64 encoding?

Mo.
Mo.Mo.
13.6k29 gold badges70 silver badges118 bronze badges

11 Answers

Some browsers such as Firefox, Chrome, Safari, Opera and IE10+ can handle Base64 natively. Take a look at this Stackoverflow question. It's using btoa() and atob() functions.

For server-side JavaScript, there's a btoa package for Node.JS.

If you are going for a cross-browser solution, there are existing libraries like CryptoJS or code like:

With the latter, you need to thoroughly test the function for cross browser compatibility. And error has already been reported.

Community
Ranhiru Jude CoorayRanhiru Jude Cooray
14.1k16 gold badges74 silver badges119 bronze badges

In Gecko/WebKit-based browsers (Firefox, Chrome and Safari) and Opera, you can use btoa() and atob().

Original answer: How can you encode a string to Base64 in JavaScript?

Community
nyuszika7hnyuszika7h
8,8004 gold badges36 silver badges47 bronze badges

Internet Explorer 10+

Cross-Browser

with Node.js

Here is how you encode normal text to base64 in Node.js:

And here is how you decode base64 encoded strings:

with Dojo.js

To encode an array of bytes using dojox.encoding.base64:

To decode a base64-encoded string:

bower install angular-base64

But How?

If you would like to learn more about how base64 is encoded in general, and in JavaScript in-particular, I would recommend this article: Computer science in JavaScript: Base64 encoding

davidcondreydavidcondrey
24.4k7 gold badges87 silver badges119 bronze badges

Here is a tightened up version of Sniper's post. It presumes well formed base64 string with no carriage returns. This version eliminates a couple of loops, adds the &0xff fix from Yaroslav, eliminates trailing nulls, plus a bit of code golf.

broc.seibbroc.seib
15.7k6 gold badges51 silver badges52 bronze badges

Short and fast Base64 JavaScript Decode Function without Failsafe:

msrd0
4,5366 gold badges27 silver badges51 bronze badges
SniperSniper

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

ceejayozceejayoz
148k35 gold badges232 silver badges316 bronze badges
leaf
11.9k5 gold badges42 silver badges71 bronze badges
PPBPPB

Did someone say code golf? =)

The following is my attempt at improving my handicap while catching up with the times. Supplied for your convenience.

What I was actually after was an asynchronous implementation and to my surprise it turns out forEach as opposed to JQuery's $([]).each method implementation is very much synchronous.

If you also had such crazy notions in mind a 0 delay window.setTimeout will run the base64 decode asynchronously and execute the callback function with the result when done.

@Toothbrush suggested 'index a string like an array', and get rid of the split. This routine seems really odd and not sure how compatible it will be, but it does hit another birdie so lets have it.

While trying to find more information on JavaScript string as array I stumbled on this pro tip using a /./g regex to step through a string. This reduces the code size even more by replacing the string in place and eliminating the need of keeping a return variable.

Best Text Base For Javascript Mac

If however you were looking for something a little more traditional perhaps the following is more to your taste.

I didn't have the trailing null issue so this was removed to remain under par but it should easily be resolved with a trim() or a trimRight() if you'd prefer, should this pose a problem for you.

ie.

Note:

Base

The result is an ascii byte string, if you need unicode the easiest is to escape the byte string which can then be decoded with decodeURIComponent to produce the unicode string.

Since escape is being deprecated we could change our function to support unicode directly without the need for escape or String.fromCharCode we can produce a % escaped string ready for URI decoding.

nJoy!

nickl-nickl-
5,7632 gold badges34 silver badges44 bronze badges

I have tried the Javascript routines at phpjs.org and they have worked well.

I first tried the routines suggested in the chosen answer by Ranhiru Cooray - http://ntt.cc/2008/01/19/base64-encoder-decoder-with-javascript.html

I found that they did not work in all circumstances. I wrote up a test case where these routines fail and posted them to GitHub at:

I also posted a comment to the blog post at ntt.cc to alert the author (awaiting moderation - the article is old so not sure if comment will get posted).

Scott CarterScott Carter

I'd rather use the bas64 encode/decode methods from CryptoJS, the most popular library for standard and secure cryptographic algorithms implemented in JavaScript using best practices and patterns.

Dan DascalescuDan Dascalescu
76.5k23 gold badges217 silver badges297 bronze badges
KARTHIKEYAN.AKARTHIKEYAN.A

Best Text Based For Javascript Mac

6,4703 gold badges52 silver badges60 bronze badges

Download Javascript For Mac

Not the answer you're looking for? Browse other questions tagged javascriptbase64 or ask your own question.