base-x-array

3.1.1 • Public • Published

base-x

Fast base encoding / decoding of any given alphabet using bitcoin style leading zero compression.

Example

Base58

Alphabets

See below for a list of commonly recognized alphabets, and their respective base.

Base Alphabet
2 `01`
8 `01234567`
11 `0123456789a`
16 `0123456789abcdef`
32 `0123456789ABCDEFGHJKMNPQRSTVWXYZ`
36 `0123456789abcdefghijklmnopqrstuvwxyz`
58 `123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz`
62 `0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ`
64 `ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/`
66 `ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.!~`

How it works

It encodes octet arrays by doing long divisions on all significant digits in the array, creating a representation of that number in the new base. Then for every leading zero in the input (not significant as a number) it will encode as a single leader character. This is the first in the alphabet and will decode as 8 bits. The other characters depend upon the base. For example, a base58 alphabet packs roughly 5.858 bits per character.

This means the encoded string 000f (using a 0-f alphabet) will actually decode to 4 bytes unlike a typical hex codec which uniformly packs 4 bits into each character.

While unusual, this does mean that no padding is required and it works for bases like 43. If you need standard hex encoding or base64 encoding you probably don't want this.

The algorithm used to convert the base of the number is roughly this:

Of course the input is actually an array of digits already :)

Using base-x in Legacy Versions of Node (below 4.5)

If you are using base-x in Node versions below `4.5.0`, please use `base-x ^2.0.0`. `base-x ^3.0.0` uses `Buffer.from` which is not supported prior to Node `4.5.0`.

Package Sidebar

Install

`npm i base-x-array`

6

3.1.1