Decode Base62

About Base62

What is Base62?

Base62 is a compact binary-to-text encoding that uses digits, uppercase letters, and lowercase letters.

Why use Base62?

Base62 is useful for short text identifiers because it avoids punctuation while staying more compact than hexadecimal.

Base62 example

Hello -> 5TP3P3v
5TP3P3v -> Hello

Base62 in JavaScript

// npm install base-x
import basex from 'base-x';

const base62 = basex('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz');

const encoded = base62.encode(Buffer.from('Hello'));
console.log(encoded);
// Output: 5TP3P3v

const decoded = Buffer.from(base62.decode('5TP3P3v')).toString('utf8');
console.log(decoded);
// Output: Hello
				

Base62 in Go

package main

import (
	"fmt"

	"github.com/eknkc/basex"
)

func main() {
	base62, _ := basex.NewEncoding("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")

	encoded := base62.Encode([]byte("Hello"))
	fmt.Println(encoded)
	// Output: 5TP3P3v

	decoded, _ := base62.Decode(encoded)
	fmt.Println(string(decoded))
	// Output: Hello
}
				

Base62 in PHP

<?php
// composer require tuupola/base62
require 'vendor/autoload.php';

$base62 = new Tuupola\Base62;

$encoded = $base62->encode("Hello");
echo $encoded . "\n";

$decoded = $base62->decode($encoded);
echo $decoded . "\n";
// Output: Hello