GithubHelp home page GithubHelp logo

javiercejudo / base-conversion Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 0.0 30 KB

Convert between arbitrary bases

Home Page: https://www.npmjs.com/package/base-conversion

License: MIT License

JavaScript 96.41% Shell 3.59%
conversion base radix base-conversion

base-conversion's Introduction

base-conversion

Build Status Coverage Status Code Climate

Auto-curried converter between any bases with arbitrary precision support and customisable symbols

Install

npm i base-conversion

Basic usage

var bc = require('base-conversion');
var hexToBin = bc(16, 2);

hexToBin('A'); //=> '1010'
hexToBin('1E'); //=> '11110'

See spec.

Custom symbols

For any bases above 62, custom symbols are required. See tests for working examples.

var bc = require('base-conversion');

bc.symbols('⓿①②③④⑤⑥⑦⑧⑨ⒶⒷ', 8, 12, '⑦③'); //=> '④Ⓑ'

Symbol translation

To convert between different sets of symbols, use bc.translate:

var bc = require('base-conversion');
var pipe = require('ramda/src/pipe');

var hexToDuoNormal = bc(16, 12);
var hexToDuoCustom = pipe(hexToDuo, bc.translate('0123456789ᘔƐ'));

hexToDuoNormal('10B'); // => '1A3'
hexToDuoCustom('10B'); // => '1ᘔ3'

To use it to translate from custom symbols, use bc.translateRaw:

var mySymbols = '⓿①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮';
var myTranslate = bc.translateRaw(mySymbols);

var hexToDuoFancy = bc.symbols(mySymbols, 16, 12);
var hexToDuoCustom = pipe(hexToDuoFancy, myTranslate('0123456789ᘔƐ'));

hexToDuoFancy('①⓿⑪'); //=> '①⑩③'
hexToDuoCustom('①⓿⑪'); //=> '1ᘔ3'

Arbitrary precision

var bc = require('base-conversion');
var Big = require('arbitrary-precision')(require('bigjs-adapter'));
var toBigFactory = require('to-decimal-arbitrary-precision');

var d = toBigFactory(Big);

// avoid large numbers to go into exponential notation (adapter dependent)
Big.Impl.E_POS = 50;

bc.big(d, 10, 9, '5678364565345634563456346757364563534534645745');
//=> '802531310452364303450750087576673257456135727727'

// equivalent but it skips a trivial decimal to decimal conversion
bc.fromDecimal.big(d, 9, '5678364565345634563456346757364563534534645745');
//=> '802531310452364303450750087576673257456135727727'

Full raw version

var bc = require('base-conversion');
var Big = require('arbitrary-precision')(require('bigjs-adapter'));
var toBigFactory = require('to-decimal-arbitrary-precision');

var d = toBigFactory(Big);

// avoid large numbers to go into exponential notation (adapter dependent)
Big.Impl.E_POS = 50;

bc.raw(d, '01234#6789', 10, 9, '#678364#6#34#634#634#63467#7364#63#34#3464#74#');
//=> '802#313104#23643034#07#0087#766732#74#613#727727'

// equivalent
bc.fromDecimal.raw(d, '01234#6789', 9, '#678364#6#34#634#634#63467#7364#63#34#3464#74#');
//=> '802#313104#23643034#07#0087#766732#74#613#727727'

Defaults

The default symbols and big implementation are exposed as follows:

bc.defaultSymbols; //=> '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
bc.defaultB; //=> default arbitrary precision implementation (plus, times, div, mod & pow)

base-conversion's People

Contributors

greenkeeperio-bot avatar javiercejudo avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

base-conversion's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.