base62

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package base62 is a standard-library-only Go implementation of Base62 encoding, the alphanumeric (0-9, A-Z, a-z) binary-to-text scheme used by URL shorteners and short-id generators across the npm ecosystem (base62, base-x). Because the alphabet contains only characters that are safe in URLs and identifiers, Base62 is a common choice for compact, human-friendly tokens.

Encode and Decode convert between arbitrary byte slices and Base62 strings, preserving leading zero bytes as leading '0' characters. EncodeInt and DecodeInt convert unsigned 64-bit integers, the form most short-id schemes use. Decode and DecodeInt return ErrInvalidCharacter for input outside the alphabet. The implementation uses math/big for the base conversion, is deterministic, and depends only on the standard library.

Index

Constants

View Source
const Alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

Alphabet is the standard Base62 alphabet: digits, then uppercase, then lowercase letters.

Variables

View Source
var ErrInvalidCharacter = errors.New("base62: invalid character")

ErrInvalidCharacter is returned when decoding input containing a character outside the Base62 alphabet.

Functions

func Decode

func Decode(s string) ([]byte, error)

Decode returns the bytes represented by the Base62-encoded string s. Leading '0' characters decode to leading zero bytes. It returns ErrInvalidCharacter for any character outside the alphabet.

func DecodeInt

func DecodeInt(s string) (uint64, error)

DecodeInt returns the unsigned integer represented by the Base62 string s. It returns ErrInvalidCharacter for input outside the alphabet.

func Encode

func Encode(input []byte) string

Encode returns the Base62 encoding of input. Leading zero bytes are encoded as leading '0' characters, and empty input encodes to the empty string.

func EncodeInt

func EncodeInt(n uint64) string

EncodeInt returns the Base62 encoding of an unsigned integer. Zero encodes to "0".

Types

This section is empty.

Jump to

Keyboard shortcuts

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