Giter VIP home page Giter VIP logo

simple-base-converter's Introduction

simple-base-converter

master:

Simple math module for base conversion.

Install

npm i simple-base-converter

Usage

Some bases(2, 8, 16, 32, 36, 62) are predefined.

  • _10To2(num)
  • _10To8(num)
  • _10To16(num)
  • _10To32(num)
  • _10To36(num)
  • _10To62(num)
  • _2To10(num)
  • _8To10(num)
  • _16To10(num)
  • _32To10(num)
  • _36To10(num)
  • _62To10(num)
import { _10To2, _2To10 } from "simple-base-converter";

_10To2(100); // "1100100"
_2To10(1100100); // "100"

// Input can be both integer or string number.
_10To2("100"); // "1100100"
_2To10("1100100"); // "100"

Of course you can choose bases freely.
convertBase(num, originalBase, newBase) converts num from originalBase to newBase.
If you set integer to originalBase or newBase, they are automatically converted by using default base 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.

import { convertBase } from "simple-base-converter";

convertBase("20a4", 17, 52); // "3Ag"
convertBase("20a4", "0123456789abcdefg", "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP"); // "3Ag"

Custom bases are also available.

convertBase(10000, 10, "-・"); // "・--・・・---・----"
convertBase("4649", "0123456789", "零一二三四五六七八九"); // "四**九"

simple-base-converter's People

Contributors

dependabot-preview[bot] avatar dependabot[bot] avatar rxdvd avatar sin9270 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

macil rxdvd

simple-base-converter's Issues

Exception on some cases

Hi,

For some cases it triggers an exception that I don't understand. Consider the following:

const {convertBase} = require("simple-base-converter");

const num = '9999999999';
const numBase = '0123456789';
const base = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_';

// value: '9k2-f_'
const base64 = convertBase(num, numBase, base);

// Exception: 'First augument must consist of second augument.'
const convertedBack = convertBase(base64, base, numBase);

console.log(convertedBack)

The last convert back to normal throws an exception when running in node 16.13.

Do you have any idea why this happens?

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.