Encode/Decode Strings, Bytes, and Integers With Base62

Category: Javascript , Recommended | September 16, 2025
Authorsindresorhus
Last UpdateSeptember 16, 2025
LicenseMIT
Tags
Views91 views
Encode/Decode Strings, Bytes, and Integers With Base62

base62 is a lightweight Javascript library for encoding and decoding strings, bytes, and integers into Base62 format.

This is perfect for situations where you need to represent large values in a shorter, more readable format. Think URL shortening, creating unique codes, or just making data more compact.

The base62 encoding scheme uses 62 characters. The characters consist of the capital letters A-Z, the lower case letters a-z and the numbers 0–9. It is a binary-to-text encoding scheme that represents binary data in an ASCII string format. Learn more on Wikipedia.

How to use it:

1. Install and import the base62 with NPM.

npm install @sindresorhus/base62
import base62 from '@sindresorhus/base62';

2. Use the following functions to encode and decode various data types:

// => 7dG5ZiXHxFjsJQ
base62.encodeString('CSS Script');
// => CSS Script
base62.decodeString('7dG5ZiXHxFjsJQ');
// => 3D7
base62.encodeInteger(12345);
// => 12345
base62.decodeInteger(3D7);
// => 28i0O
console.log(base62.encodeString('✨'));
// => ✨
console.log(base62.decodeString('28i0O'));
// encode bytes to a Base62 string.
encodeBytes(bytes: Uint8Array): string
// decode a Base62 string back to bytes
decodeBytes(encodedString: string): Uint8Array

3. The library also supports encoding and decoding BigInt values, which is useful in applications requiring handling of very large numbers.

encodeBigInt(integer: bigint)
decodeBigInt(encodedString: string)

4. You’re now allowed to customize alphabet since v1.0.0:

const customAlphasbet = new Base62({
  alphabet: 'ABC123'
});
console.log(customAlphasbet.encodeInteger(1234));

Changelog:

v1.0.0 (09/16/2025)

  • Add custom alphabet support

You Might Be Interested In:


Leave a Reply