解码 Base85

关于 Base85

什么是 Base85?

Base85 也称为 Ascii85,用五个可打印 ASCII 字符表示四个字节。此工具遵循 Adobe 风格的 Ascii85 行为。

为什么使用 Base85?

Base85 比 Base64 更紧凑,在需要将二进制数据嵌入为可打印文本时很有用。

Base85 示例

Hello -> 87cURDZ
<~87cURDZ~> -> Hello

JavaScript 中的 Base85

// npm install ascii85
const ascii85 = require('ascii85');

const encoded = ascii85.encode(Buffer.from('Hello')).toString();
console.log(encoded);
// Output: 87cURDZ

const decoded = ascii85.decode('87cURDZ').toString();
console.log(decoded);
// Output: Hello
				

Go 中的 Base85

package main

import (
	"encoding/ascii85"
	"fmt"
)

func main() {
	dst := make([]byte, ascii85.MaxEncodedLen(len("Hello")))
	n := ascii85.Encode(dst, []byte("Hello"))
	encoded := string(dst[:n])
	fmt.Println(encoded)
	// Output: 87cURDZ

	decoded := make([]byte, len("87cURDZ"))
	n, _, _ = ascii85.Decode(decoded, []byte("87cURDZ"), true)
	fmt.Println(string(decoded[:n]))
	// Output: Hello
}
				

PHP 中的 Base85

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

$base85 = new Tuupola\Base85;

$encoded = $base85->encode("Hello");
echo $encoded . "\n";
// Output: 87cURDZ

$decoded = $base85->decode("87cURDZ");
echo $decoded . "\n";
// Output: Hello