Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
@cheprasov/base-converter
Advanced tools
The library allows to convert a number between arbitrary bases
The library allows to convert a number between arbitrary bases.
Before using the library please check native method Number.toString([radix])
, maybe this is what you need.
The library was written mostly for converting more complicated or custom bases.
import { BaseConverter, baseBinary, baseHexadecimal } from '@cheprasov/base-converter';
// encode
console.log('7 dec =>', BaseConverter.encode(7, baseBinary), 'bin');
// 7 dec => 111 bin
console.log('255 dec =>', BaseConverter.encode(255, baseHexadecimal), 'hex');
// 255 dec => FF hex
console.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 dec
console.log('FA hex =>', BaseConverter.decode('FA', baseHexadecimal), 'dec');
// FA hex => 250 dec
console.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 hex
console.log('FF hex =>', BaseConverter.convert('FF', baseHexadecimal, baseBinary), 'bin');
// FF hex => 11111111 bin
console.log('FF custom =>', BaseConverter.convert('FF', ['A', 'B', 'C', 'D', 'E', 'F'], ['1', '2', '3']), 'custom');
// FF custom => 2133 bin
> npm install @cheprasov/base-converter
// import library
import { BaseConverter } from '@cheprasov/base-converter';
// import preset bases
import {
baseBinary,
baseOctal,
baseDecimal,
baseHexadecimal,
base62
} from '@cheprasov/base-converter';
A base has type Array<string|number>
, it means that each base is array of integers or strings. Also, each element is the array should have 1 symbol only.
Examples:
// 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
encode(value: number, base: Array<string | number>): string | null
Converts decimal number to provided base. Will return null
if value can not be converted.
Example:
console.log('7 dec =>', BaseConverter.encode(7, baseBinary), 'bin');
// 7 dec => 111 bin
console.log('255 dec =>', BaseConverter.encode(255, baseHexadecimal), 'hex');
// 255 dec => FF hex
console.log('255 dec =>', BaseConverter.encode(255, ['A', 'B', 'C']), 'custom');
// 255 dec => BAABBA custom
decode(value: string, base: Array<string | number>): number | null
Converts value from provided base to decimal number. Will return null
if value can not be converted.
Example:
console.log('101 bin =>', BaseConverter.decode(101, baseBinary), 'dec');
// 101 bin => 5 dec
console.log('FA hex =>', BaseConverter.decode('FA', baseHexadecimal), 'dec');
// FA hex => 250 dec
console.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.
console.log('1010 bin =>', BaseConverter.convert(1010, baseBinary, baseHexadecimal), 'hex');
// 1010 bin => A hex
console.log('FF hex =>', BaseConverter.convert('FF', baseHexadecimal, baseBinary), 'bin');
// FF hex => 11111111 bin
console.log('FF custom =>', BaseConverter.convert('FF', ['A', 'B', 'C', 'D', 'E', 'F'], ['1', '2', '3']), 'custom');
// FF custom => 2133 bin
Feel free to fork project, fix bugs, tests and finally request for pull
FAQs
The library allows to convert a number between arbitrary bases
We found that @cheprasov/base-converter demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.