base62

package module
v1.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 25, 2026 License: MIT Imports: 1 Imported by: 0

README

base62

Custom Base-62 Encoder

GitHub tag (latest by date) GitHub last commit GitHub issues npm NPM

This repository contains the version of my encoder/decoder for Base-62 in three languages:

The latter could be downloaded from its NPM repo: https://www.npmjs.com/package/base62-ts.

Motivation

I needed an efficient way to apply a Base-62 encoding/decoding algorithm working the same way in TypeScript/Javascript, Python and Golang environments.

WARNING: Decoding any wrongful string returns an error and 0. You shouldn't use the latter.

Usage

Both versions use the same following dictionary:

Value Character Value Character Value Character Value Character Value Character
0 0 13 d 26 q 39 D 52 Q
1 1 14 e 27 r 40 E 53 R
2 2 15 f 28 s 41 F 54 S
3 3 16 g 29 t 42 G 55 T
4 4 17 h 30 u 43 H 56 U
5 5 18 i 31 v 44 I 57 V
6 6 19 j 32 w 45 J 58 W
7 7 20 k 33 x 46 K 59 X
8 8 21 l 34 y 47 L 60 Y
9 9 22 m 35 z 48 M 61 Z
10 a 23 n 36 A 49 N
11 b 24 o 37 B 50 O
12 c 25 p 38 C 51 P

In other words, they use the following base: 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.

For TypeScript:

npm i base62-ts
import * as base62 from 'base62-ts'

const value = 18969

const encoded = base62.encode(value)

// 4VX
console.log(encoded)

const decoded = base62.decode(encoded)

console.assert(value === decoded)

console.assert(base62.isValid("4VX"))

For Go:

go get github.com/cyrildever/base62
import "github.com/cyrildever/base62"

value := "4VX"

decoded, err := base62.Decode(value)

// 18969
fmt.Println(decoded)

encoded, err := base62.Encode(decoded)

assert.Equal(t, value, encoded)

assert.Assert(t, base62.IsValid("4VX"))

For Python:

pip install base62-py
from base62 import decode, encode, is_valid_base62

value = 18969

encoded = encode(value)

# 4VX
print(encoded)

decoded = decode(encoded)

assert value == decoded

assert is_valid_base62(encoded)
License

All versions are available under a MIT license (see LICENSE).


© 2021-2026 Cyril Dever. All rights reserved.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode(value string) (uint, error)

Decode ...

func Encode

func Encode(value uint) (string, error)

Encode ...

func IsValid added in v1.3.16

func IsValid(input string) bool

IsValid ...

Types

type Base62 added in v1.1.0

type Base62 string

Base62 is a base62 string representation of a byte array. NB: if the passed string is not a valid base62 representation, it will not throw an error but rather returns empty or nil items when methods are called.

const ERROR Base62 = Base62("%") // TODO Make more robust

func ToBase62 added in v1.1.0

func ToBase62(value int) Base62

ToBase62 ...

func (Base62) Bytes added in v1.1.0

func (b Base62) Bytes() []byte

Bytes ...

func (Base62) IsEmpty added in v1.1.0

func (b Base62) IsEmpty() bool

IsEmpty ...

func (Base62) NonEmpty added in v1.1.0

func (b Base62) NonEmpty() bool

NonEmpty ...

func (Base62) String added in v1.1.0

func (b Base62) String() string

String ...

func (Base62) Value added in v1.1.0

func (b Base62) Value() int

Value returns `-1` if the string is invalid, the actual integer value otherwise

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL