base-p

Convert to/from integer in any base

Usage no npm install needed!

<script type="module">
  import baseP from 'https://cdn.skypack.dev/base-p';
</script>

README

Install

npm install base-p

Usage

const Base = require('base-p')

base62 = new Base(62)
base62.encode(2017) // returns 'WX'
base62.decode('WX') // returns 2017

base5 = new Base('01234')
base5.encode(7)     // returns '12'
base5.decode('12')  // returns 7

Usage with BigInt

const Base = require('base-p')

base62 = new Base(62)
base62.encodeBig(2017n) // returns 'WX'
base62.decodeBig('WX')  // returns 2017n

base5 = new Base('01234')
base5.encodeBig(7n)    // returns '12'
base5.decodeBig('12')  // returns 7n

Base

The parameter for the constructor is a either a string containing the alphabet used for the conversion, or an integer representing the base for one of the predefined alphabets:

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