Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/cheprasov/js-base-converter
The library allows to convert a number between arbitrary bases.
https://github.com/cheprasov/js-base-converter
binary converter decimal hexadecimal octal
Last synced: about 1 month ago
JSON representation
The library allows to convert a number between arbitrary bases.
- Host: GitHub
- URL: https://github.com/cheprasov/js-base-converter
- Owner: cheprasov
- Created: 2018-08-18T20:11:53.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2021-06-10T19:42:51.000Z (over 3 years ago)
- Last Synced: 2024-11-01T05:04:45.845Z (2 months ago)
- Topics: binary, converter, decimal, hexadecimal, octal
- Language: JavaScript
- Homepage:
- Size: 16.6 KB
- Stars: 4
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
Awesome Lists containing this project
README
[![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](http://opensource.org/licenses/MIT)
@cheprasov/base-converter
=========The library allows to convert a number between arbitrary bases.
## Note
Before using the library please check native method [`Number.toString([radix])`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toString), maybe this is what you need.
The library was written mostly for converting more complicated or custom bases.## Quick examples:
```javascriptimport { BaseConverter, baseBinary, baseHexadecimal } from '@cheprasov/base-converter';
// encode
console.log('7 dec =>', BaseConverter.encode(7, baseBinary), 'bin');
// 7 dec => 111 binconsole.log('255 dec =>', BaseConverter.encode(255, baseHexadecimal), 'hex');
// 255 dec => FF hexconsole.log('255 dec =>', BaseConverter.encode(255, ['A', 'B', 'C']), 'custom');
// 255 dec => BAABBA custom// decode
console.log('101 bin =>', BaseConverter.decode(101, baseBinary), 'dec');
// 101 bin => 5 decconsole.log('FA hex =>', BaseConverter.decode('FA', baseHexadecimal), 'dec');
// FA hex => 250 decconsole.log('BAA custom =>', BaseConverter.decode('BAA', ['A', 'B', 'C']), 'dec');
// BAA custom => 9 dec// convert
console.log('1010 bin =>', BaseConverter.convert(1010, baseBinary, baseHexadecimal), 'hex');
// 1010 bin => A hexconsole.log('FF hex =>', BaseConverter.convert('FF', baseHexadecimal, baseBinary), 'bin');
// FF hex => 11111111 binconsole.log('FF custom =>', BaseConverter.convert('FF', ['A', 'B', 'C', 'D', 'E', 'F'], ['1', '2', '3']), 'custom');
// FF custom => 2133 bin
```## How to install
```bash
> npm install @cheprasov/base-converter
```## Documentation
### Import
```javascript
// import library
import { BaseConverter } from '@cheprasov/base-converter';// import preset bases
import {
baseBinary,
baseOctal,
baseDecimal,
baseHexadecimal,
base62
} from '@cheprasov/base-converter';
```### Custom base
A base has type `Array`, it means that each base is array of integers or strings. Also, each element is the array should have 1 symbol only.Examples:
```javascript// correct
const base4 = [0, 1, 2, 3];
const base8 = [0, 1, 2, 3, 'W', 'X', 'Y', 'Z'];
const baseSome = ['0', 'a', 'A', 'b', 'B'];
const baseSymbols = ['!', '@', '#', '$', '%', '^', '&', '*', '.', '-', '+', '='];// wrong
const base4 = [0, 10, 20, 30]; // should be 1 symbols for each element
const base8 = [0, 1, 2, 3, 'AA', 'BB', 'CC', 'DD']; // should be 1 symbols for each element
const baseSome = ['0', '1', '1', '2', '2']; // duplicates of items
const baseSome = ['A']; // should be more then 1 element in the array
```### Public methods
#### `encode(value: number, base: Array): string | null`
Converts decimal number to provided base. Will return `null` if value can not be converted.Example:
```javascript
console.log('7 dec =>', BaseConverter.encode(7, baseBinary), 'bin');
// 7 dec => 111 binconsole.log('255 dec =>', BaseConverter.encode(255, baseHexadecimal), 'hex');
// 255 dec => FF hexconsole.log('255 dec =>', BaseConverter.encode(255, ['A', 'B', 'C']), 'custom');
// 255 dec => BAABBA custom
```#### `decode(value: string, base: Array): number | null`
Converts value from provided base to decimal number. Will return `null` if value can not be converted.Example:
```javascript
console.log('101 bin =>', BaseConverter.decode(101, baseBinary), 'dec');
// 101 bin => 5 decconsole.log('FA hex =>', BaseConverter.decode('FA', baseHexadecimal), 'dec');
// FA hex => 250 decconsole.log('BAA custom =>', BaseConverter.decode('BAA', ['A', 'B', 'C']), 'dec');
// BAA custom => 9 dec
```#### `convert(value: string | number, fromBase: BaseType, toBase: BaseType): string | null`
Converts value from one base to another base. Will return `null` if value can not be converted.```javascript
console.log('1010 bin =>', BaseConverter.convert(1010, baseBinary, baseHexadecimal), 'hex');
// 1010 bin => A hexconsole.log('FF hex =>', BaseConverter.convert('FF', baseHexadecimal, baseBinary), 'bin');
// FF hex => 11111111 binconsole.log('FF custom =>', BaseConverter.convert('FF', ['A', 'B', 'C', 'D', 'E', 'F'], ['1', '2', '3']), 'custom');
// FF custom => 2133 bin
```## Something does not work
Feel free to fork project, fix bugs, tests and finally request for pull