Skip to content
This repository has been archived by the owner on Jul 6, 2022. It is now read-only.

nodecfdi/utils-internals-baseconverter

Repository files navigation

Note: @nodecfdi/utils-internals-baseconverter is now deprecated. Use @nodecfdi/base-converter.

@nodecfdi/utils-internals-baseconverter

Source Code Software License Latest Version Discord

TS utility for converting an input from any base to another base the min base this library accepts is 2 and max base is 36.

Main features

  • Convert an input from any valid base to another.

Installation

npm i @nodecfdi/utils-internal-baseconverter --save

or

yarn add @nodecfdi/utils-internal-baseconverter

Implementation

The converter expects 3 parameters: converter.convert(input, fromBase, toBase)

import { BaseConverter } from '@nodecfdi/utils-internal-baseconverter';
// this is the main reason to exists of BaseConverter class
// since parseInt and toString cannot handle large inputs
const input = '3330303031303030303030333030303233373038';
const converter = BaseConverter.createBase36();
// result will be: 292233162870206001759766198425879490508935868472
const result = converter.convert(input, 16, 10);

alternatively you can define your own sequence:

import { BaseConverter, BaseConverterSequence } from '@nodecfdi/utils-internal-baseconverter';
const hexSequence = new BaseConverterSequence('0123456789ABCDEF');
const converter = new BaseConverter(hexSequence);
const input = 'FFFF';
// resut will be: 1111111111111111
converter.convert(input, 16, 2);