New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

basex-converter

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

basex-converter

Decode/encode hex,binary,regular values in any base and Base Converter

latest
Source
npmnpm
Version
1.0.1
Version published
Maintainers
1
Created
Source

basex-converter

NPM Package NPM License npm bundle size NPM Download

Due to the limitation of JavaScript's integer representation, which is limited to 53 bits, handling large numbers for encryption tasks can be challenging. Existing libraries run into problems when dealing with such values;
To fix this limitation, This library provides encoding of large numbers. This library provides the following features:

Encoding and decoding of various data types:

  • Regular numerical values
  • Hexadecimal values
  • Binary data as Uint8Array Base Conversion: This library provides a multipurpose convert_base() function that can convert a value from any base to another base without rounding off and size restriction

By overcoming JavaScript's integer limitations, this library allows developers to efficiently work with large numbers in encoding and base conversion tasks.

[!CAUTION]
Be careful, spaces(" ") in the whole value and zeros(0) at the beginning of the value being ignored

Default alphabet: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
Inverted alphabet: "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
Default base: 62

Alphabets

See below for a list of commonly recognized alphabets, and their respective base.

BaseImport NameAlphabet
2ALPHABET_BASE201
8ALPHABET_BASE801234567
11ALPHABET_BASE110123456789a
16ALPHABET_BASE160123456789abcdef
32ALPHABET_BASE320123456789ABCDEFGHJKMNPQRSTVWXYZ
36ALPHABET_BASE360123456789abcdefghijklmnopqrstuvwxyz
58ALPHABET_BASE58123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz
62ALPHABET_BASE62_Default0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
64ALPHABET_BASE64ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
67ALPHABET_BASE67ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.!~

Getting started

Installation

npm install basex-converter

Alternatively using Yarn:

yarn add basex-converter

Usage

Import

import base62 from 'basex-converter';
// or for making instance with custom alphabet
import { Base62, ALPHABET_BASE62_Default, ALPHABET_BASE62_INVERTED, BASE_DEFAULT } from 'basex-converter';

Commonjs require

const base62 = require('basex-converter').default;
const { Base62, ALPHABET_BASE62_Default, ALPHABET_BASE62_INVERTED, BASE_DEFAULT } = require('basex-converter');
console.log(base62.decode('base62'));
// '34441886726'
console.log(base62.encode('34441886726'));
// 'base62'

Encode and decode bytes

console.log(base62.encode_bytes(new Uint8Array([0x01, 0x01])));
// '49'
console.log(base62.decode_bytes('49'));
// Uint8Array(2) [ 1, 1 ]

Encode and decode hex

This function is useful to encode and decode MongoDB ObjectId too

console.log(base62.encode_hex('604a38563'));
// 'SDG16R'
console.log(base62.decode_hex('SDG16R'));
// '604a38563'

// encode and decode mongodb objectid
console.log(base62.encode_hex('63d91de18f092ab964484b9e'));
// 'eBhQNIyMqR6WH3XS'
console.log(base62.decode_hex('eBhQNIyMqR6WH3XS'));
// '63d91de18f092ab964484b9e'

Base converter

This function is able to convert any base to another with no limit on the size of value

console.log(base62.convert_base('ff', 16, 10));
// '255'
console.log(base62.convert_base('wiv', 36, 10));
// '42151'
console.log(base62.convert_base('123456789123456789123456789123456789123456789', 10, 16));
// '58936e53d139afefabb2683f150b684045f15'

This library provides the ability to change the alphabet and base for encoding and decoding. However, changing the base is not recommended unless you are sure that the chosen base works without malfunctioning.
But the alphabet can be changed without functional problems. it just sensitive to "space" character, and we can not use it.

import { Base62, ALPHABET_BASE58 } from 'basex-converter';
// make instance
const base62 = new Base62();
// make another base
const base62 = new Base62(ALPHABET_BASE58, 58);
// or you can set alphabet
const base62 = new Base62('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz');
// or base
const base62 = new Base62('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', 62);

Can pass custom alphabet for each converting

console.log(base62.decode('base62', '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'));
// '34441886726'
console.log(base62.encode('34441886726', '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'));
// 'base62'

Change alphabet or base, for changing base you should set a compatible alphabet too

import base62, { ALPHABET_BASE58, ALPHABET_BASE62_INVERTED } from 'basex-converter';
// use costom alphabet
base62.setAlphabet('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz');
// or use existing ones
base62.setAlphabet(ALPHABET_BASE62_INVERTED);
base62.setBaseAlphabet(ALPHABET_BASE58, 58);

Keywords

base62

FAQs

Package last updated on 24 Jun 2025

Did you know?

Socket

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.

Install

Related posts