解码 Base62

关于 Base62

什么是 Base62?

Base62 是一种紧凑的二进制到文本编码,使用数字、大写字母和小写字母。

为什么使用 Base62?

Base62 适合短文本标识符,因为它避免使用标点符号,并且比十六进制更紧凑。

Base62 示例

Hello -> 5TP3P3v
5TP3P3v -> Hello

JavaScript 中的 Base62

// 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
				

Go 中的 Base62

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
}
				

PHP 中的 Base62

<?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