# power-radix

> Library for converting numbers from one radix representation (encoding) to another, with optional custom defined encodings. Inspired by rubyworks/radix.

Latest version **3.0.1** (published 2023-09-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install power-radix
pnpm add power-radix
yarn add power-radix
bun add power-radix
```

## Health

**Score 23/100 (F)** — status: abandoned.

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.1 |
| Published | 2023-09-12 |
| First published | 2015-03-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/power-radix) |
| Module format | CommonJS |
| Node | >= 10.0.0 |
| Dependencies | 0 |
| Unpacked size | 26.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 14 |
| Author | Casey Flynn |
| Maintainers | cflynn07 |
| Keywords | arithmetic, base conversion, base convert, base, base10, base58 decode, base58 encode, base58, base64 decode, base64 encode, base64, conversion, convert, decode, encode, hex, math, power, radix conversion, radix convert, radix |

## Links

- npm: https://www.npmjs.com/package/power-radix
- Repository: https://github.com/cflynn07/power-radix
- Issues: https://github.com/cflynn07/power-radix/issues
- npm.io page: https://npm.io/package/power-radix

## Alternatives

- [flatbuffers](https://npm.io/package/flatbuffers.md) — 6.0M weekly downloads
- [jwt-simple](https://npm.io/package/jwt-simple.md) — 259.5K weekly downloads
- [@exodus/patch-broken-hermes-typed-arrays](https://npm.io/package/@exodus/patch-broken-hermes-typed-arrays.md) — 28.5K weekly downloads
- [@native-to-anchor/buffer-layout](https://npm.io/package/@native-to-anchor/buffer-layout.md) — 12.2K weekly downloads
- [binary-parser-encoder](https://npm.io/package/binary-parser-encoder.md) — 5.3K weekly downloads

## Recent versions

- 3.0.1 (latest) — 2023-09-12
- 3.0.0 — 2023-09-12
- 2.5.6 — 2020-03-21
- 2.5.5 — 2020-03-16
- 2.5.4 — 2020-01-20
- 2.5.3 — 2019-12-30
- 2.5.2 — 2019-12-29
- 2.5.1 — 2015-12-31
- 2.5.0 — 2015-12-31
- 2.4.0 — 2015-12-24
- 2.3.3 — 2015-10-13
- 2.3.2 — 2015-10-13
- 2.3.1 — 2015-10-13
- 2.3.0 — 2015-10-13
- 2.2.1 — 2015-07-04
- … 7 more at https://npm.io/package/power-radix/versions

## README

power-radix
===========
[![Build Status](https://github.com/cflynn07/power-radix/workflows/Publish/badge.svg)](https://github.com/cflynn07/power-radix/actions)
[![npm version](https://badge.fury.io/js/power-radix.svg)](https://badge.fury.io/js/power-radix)
[![Code Climate](https://codeclimate.com/github/cflynn07/power-radix/badges/gpa.svg)](https://codeclimate.com/github/cflynn07/power-radix)
[![codecov](https://codecov.io/gh/cflynn07/power-radix/branch/master/graph/badge.svg)](https://codecov.io/gh/cflynn07/power-radix)
[![Dependency Status](https://david-dm.org/cflynn07/power-radix.svg)](https://david-dm.org/cflynn07/power-radix)
[![devDependency Status](https://david-dm.org/cflynn07/power-radix/dev-status.svg)](https://david-dm.org/cflynn07/power-radix#info=devDependencies)
[![Standard - JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)

Library for converting numbers from one radix representation (encoding) to another, with optional
custom defined encodings. Inspired by rubyworks/radix.

[power-radix-encodings](https://www.npmjs.com/package/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.
```js
(255).toString(16) === 'ff'
parseInt('ff', 16) === 255
```

But JavaScript limits you to radix values 2 - 36.
```js
(255).toString(37) // error
```

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.
```js
// as an array
new PowerRadix([100, 10], 256).toArray(10); // ['2', '5', '6', '1', '0']

// or as a string
new PowerRadix([100, 10], 256).toString(10); // "25610"
```

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.
```js
var base = ['Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P'];
new PowerRadix([1, 0], 10).toArray(base); // ['W', 'Q']
new PowerRadix('10', 10).toArray(base);   // ['W', 'Q']
new PowerRadix(10, 10).toArray(base);     // ['W', 'Q']

new PowerRadix([1, 0], 10).toString(base); // "WQ"
new PowerRadix('10', 10).toString(base);   // "WQ"
new PowerRadix(10, 10).toString(base);     // "WQ"
```

Or specify an array of characters to use as symbols for the base radix
```js
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);   // [1, 0]
new PowerRadix(['W', 'Q'], base.join('')).toArray(10); // [1, 0]
new PowerRadix(['W', 'Q'].join(''), base).toArray(stdBase10);   // [1, 0]
new PowerRadix(['W', 'Q'].join(''), base.join('')).toArray(10); // [1, 0]

new PowerRadix(['W', 'Q'], base).toString(stdBase10);   // "10"
new PowerRadix(['W', 'Q'], base.join('')).toString(10); // "10"
new PowerRadix(['W', 'Q'].join(), base).toString(stdBase10);   // "10"
new PowerRadix(['W', 'Q'].join(), base.join('')).toString(10); // "10"
```

Examples
--------
```js
// Node.js standard library crypto module
var crypto = require('crypto');

// Produce a SHA1 hash digest. SHA1 digests are 160 bits (20 bytes).
var sha1HashDigest = crypto.createHash('sha1').update('').digest('hex');
// "da39a3ee5e6b4b0d3255bfef95601890afd80709"

// When represented in radix 2 (binary), the representation is 160 characters in length
new PowerRadix(sha1HashDigest, require('power-radix-encodings/base16-hexadecimal-lowercase'))
  .toString(require('power-radix-encodings/base2-binary'));
// "1101101000111001101000111110111001011110011010110100101100001101001100100101010110111111111011111001010101100000000110001001000010101111110110000000011100001001"

// When represented in radix 10 (decimal), the representation is 49 (varies) characters in length
new PowerRadix(sha1HashDigest, require('power-radix-encodings/base16-hexadecimal-lowercase'))
  .toString(require('power-radix-encodings/base10-decimal'));
// "1245845410931227995499360226027473197403882391305"

// When represented in radix 16 (hexadecimal), the representation is 40 characters in length
new PowerRadix(sha1HashDigest, require('power-radix-encodings/base16-hexadecimal-lowercase'))
  .toString(require('power-radix-encodings/base16-hexadecimal-lowercase'));
// "da39a3ee5e6b4b0d3255bfef95601890afd80709"

var radix255CharacterEncoding = [];
for(var i = 0; i < 256; i++) { radix255CharacterEncoding.push(i+''); }

// When represented in radix 255, the representation is 20 characters in length (note:
// "characters" are actually decimal characters concatenated together)

new PowerRadix(sha1HashDigest, require('power-radix-encodings/base16-hexadecimal-lowercase'))
  .toString(radix255CharacterEncoding);
// The decimal value of each of the 20 bytes of the sha1 hash digest
//  ['218', '57', '163', '238', '94', '107', '75', '13', '50', '85', '191', '239', '149', '96', '24', '144', '175', '216', '7', '9']

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);
// The binary representations of each of the 20 bytes of the sha1 hash digest (note: each byte representation doesn't have padding leading zeros)
// ['11011010', '111001', '10100011', '11101110', '1011110', '1101011', '1001011', '1101', '110010', '1010101', '10111111', '11101111', '10010101', '1100000', '11000', '10010000', '10101111', '11011000', '111', '1001']
```

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
-------
* [Casey Flynn](http://github.com/cflynn07) - <https://cflynn.us/>

License
-------
power-radix is free and unencumbered public domain software. For more
information, see <http://unlicense.org/> or the accompanying UNLICENSE file.

---
_Source: https://npm.io/package/power-radix · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
