A simple base64 encoder. Simply pass your string and a reference to the function to get back the encoded string.
var
map ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
sample = "140bytes rocks!",
encoded = base64Encode(sample, map);
Artur Honzawa @arturhonzawa hacked the main code and further compression was done by me (@aemkei). Thanks to Rob Griffiths @bytespider and Jed Schmidt @jedschmidt for some pretty good hints!
- Proper "=" padding does not work
- IE's JScript fails with the RegExp
- Doesn't encode null bytes
Feel free to fork and improve!
Some ideas and notes:
toString(4)
andparseInt(f,4)
) instead of base-2. This does not save a byte but saves memory.f
can be omitted. Reused
instead./.../(...)
a shortcut for/.../.exec(...)
? This does not work in any of my browsers (Firefox, Opera, IE8).Here is the shortest working version I found: