power-radix
Library for converting numbers from one radix representation (encoding) to another, with optional
custom defined encodings. Inspired by rubyworks/radix.
power-radix-encodings is a useful collection
of common encodings that are independently requireable.
Features
- Convert to and from any base.
- Define custom encoding and character sets.
Installing
$ npm install power-radix
Usage
Base conversions with ASCII ordered notations are easy in Javascript.
(255).toString(16) === 'ff'
parseInt('ff', 16) === 255
But JavaScript limits you to radix values 2 - 36.
(255).toString(37)
power-radix provides the means of converting to and from any base.
For example, a number in base 256 can be representated by the array [100, 10] (Math.pow(100, 256) +
Math.pow(10, 1)) and can be converted to base 10.
new PowerRadix([100, 10], 256).toArray(10);
new PowerRadix([100, 10], 256).toString(10);
power-radix also supports custom character encodings as base and target radixes. By default,
power-radix uses the following character encoding:
0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
You can optionally specify an array of characters to use as symbols for a radix to give your output
a custom encoding.
var base = ['Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P'];
new PowerRadix([1, 0], 10).toArray(base);
new PowerRadix('10', 10).toArray(base);
new PowerRadix(10, 10).toArray(base);
new PowerRadix([1, 0], 10).toString(base);
new PowerRadix('10', 10).toString(base);
new PowerRadix(10, 10).toString(base);
Or specify an array of characters to use as symbols for the base radix
var base = ['Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P'];
var stdBase10 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
new PowerRadix(['W', 'Q'], base).toArray(stdBase10);
new PowerRadix(['W', 'Q'], base.join('')).toArray(10);
new PowerRadix(['W', 'Q'].join(''), base).toArray(stdBase10);
new PowerRadix(['W', 'Q'].join(''), base.join('')).toArray(10);
new PowerRadix(['W', 'Q'], base).toString(stdBase10);
new PowerRadix(['W', 'Q'], base.join('')).toString(10);
new PowerRadix(['W', 'Q'].join(), base).toString(stdBase10);
new PowerRadix(['W', 'Q'].join(), base.join('')).toString(10);
Examples
var crypto = require('crypto');
var sha1HashDigest = crypto.createHash('sha1').update('').digest('hex');
new PowerRadix(sha1HashDigest, require('power-radix-encodings/base16-hexadecimal-lowercase'))
.toString(require('power-radix-encodings/base2-binary'));
new PowerRadix(sha1HashDigest, require('power-radix-encodings/base16-hexadecimal-lowercase'))
.toString(require('power-radix-encodings/base10-decimal'));
new PowerRadix(sha1HashDigest, require('power-radix-encodings/base16-hexadecimal-lowercase'))
.toString(require('power-radix-encodings/base16-hexadecimal-lowercase'));
var radix255CharacterEncoding = [];
for(var i = 0; i < 256; i++) { radix255CharacterEncoding.push(i+''); }
new PowerRadix(sha1HashDigest, require('power-radix-encodings/base16-hexadecimal-lowercase'))
.toString(radix255CharacterEncoding);
var radix255BinaryCharacterEncoding = [];
for(var i = 0; i < 256; i++) { radix255BinaryCharacterEncoding.push(new PowerRadix(i, 10).toString(2)); }
new PowerRadix(sha1HashDigest, require('power-radix-encodings/base16-hexadecimal-lowercase'))
.toString(radix255BinaryCharacterEncoding);
Testing
// Tests + coverage reports are run using Lab
$ npm test
// Test coverage reports
npm run test-cov # will auto-open Google Chrome with html coverage report
Authors
License
power-radix is free and unencumbered public domain software. For more
information, see http://unlicense.org/ or the accompanying UNLICENSE file.