javascript-base64

Last updated: 2011-06-29 11:09:43 +0100

Upstream URL: git clone http://chriswarbo.net/git/javascript-base64.git

Repo

View repository

View issue tracker

Contents of README follows


Base 64 is a way of representing any data as text. The idea is simple:

<ol type="1"> <li>

All data is binary, so treat it as bits

</li> <li>

Writing out each bit individually would be incredibly inefficient, so group them into sextuplets (6-bit numbers)

</li> <li>

Grouping 6 bits gives one “digit” in base 64

</li> <li>

Choose some arbitrary symbols to represent all 64 possibilities. For example, in hexadecimal we use “ABCDEF” as the extra symbols

</li> <li>

Since most data comes in 8-bit bytes, there may be 2 or 4 bits left over at the end. Just multiply by 10 or 100 to make 6 bits

</li> <li>

Write out your base 64 number, using your chosen symbols

</li> <li>

If you multiplied by 10 in step 5, put another (arbitrary) symbol at the end

</li> <li>

If you multiplied by 100 in step 5, put 2 such symbols at the end

</li> </ol>

The symbols are indeed arbitrary, but a standardised set is used in the base 64 standards (in the same way that we standardise on “0123456789” in decimal). This simple library provides a “Base64” object with “encode” and “decode” functions. It’s Public Domain, because I was so annoyed that I couldn’t find such an obvious and useful bit of code under a permissive-enough license.

Contributions are welcome. Note that I value simplicity over efficiency, so I may choose not to include patches that just increase the speed while making it harder to understand. With that said, the simplicity is already compromised a bit since I opted to make the encode/decode functions use a single pass (otherwise the RAM usage would be unacceptable). You’re completely free to fork the code if you want to though.

<ul> <li>Chris Warburton</li> </ul>