Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Simple math module (mainly to train with npm)
JavaScript
branch: master

README.md

Base Converter Build Status

Installation

npm install base-converter

Example

var base = require('base-converter');

var n = 3598786;
var b = base.decTo62(n);
var n2 = base._62ToDec(b);
console.log(n, b, n2);

// Expected output : 3598786 'f6cW' 3598786

Methods

Predefined bases (2, 8, 16, 36, 62):

  • decToBin(n)
  • decToHex(n)
  • decToOct(n)
  • decTo36(n)
  • decTo62(n)
  • binToDec(n)
  • hexToDec(n)
  • octToDec(n)
  • _36ToDec(n)
  • _62ToDec(n)

Using your own custom base:

  • decToGeneric(n, alphabet)
  • genericToDec(n, alphabet)

Where 'alphabet' can be:

  • an integer between 2 and 36 (native conversion), or 62.
  • a string, containing all your symbols (one symbol = one character).

Example with custom base:

var bc = require('base-converter')
console.log(bc.decToGeneric(359461, 'AbcGHiuRSt'));

// Expected output : 'GitHub'
// bc.genericToDec('GitHub', base)) will return 359461 of course

Performance

Since version 1.1.0, when a base between 2 and 36 is used, we fallback to native Javascript way of converting bases:

  • decimal to base: (number).toString(base)
  • base to decimal: parseInt(string, base)

This should ensure best performance in any case.

Something went wrong with that request. Please try again.