@libn/base
Binary-to-text encoding.
b16
Base16 uses hexadecimal
characters ("0-9" and "A-F"). Encoding is always uppercase (diverging from
Number.prototype.toString
and
Uint8Array.prototype.toHex,
which use lowercase "a-f"), and decoding is case-insensitive.
import { deB16, enB16 } from "@libn/base/b16";
import { assertEquals } from "@std/assert";
const binary = new TextEncoder().encode("Hello :)");
assertEquals(enB16(binary), "48656C6C6F203A29");
assertEquals(deB16("48656C6C6F203A29"), binary);
b32
Base32 uses uppercase
letters and digits, excluding "0", "1", "8", and "9". Encoding is always
uppercase, and decoding is case-insensitive.
import { deB32, enB32 } from "@libn/base/b32";
import { assertEquals } from "@std/assert";
const binary = new TextEncoder().encode("Hello :)");
assertEquals(enB32(binary), "JBSWY3DPEA5CS");
assertEquals(deB32("JBSWY3DPEA5CS"), binary);
h32
Base32hex uses digits and
uppercase letters, excluding "W-Z". Encoding is always uppercase, and decoding
is case-insensitive.
import { deH32, enH32 } from "@libn/base/h32";
import { assertEquals } from "@std/assert";
const binary = new TextEncoder().encode("Hello :)");
assertEquals(enH32(binary), "91IMOR3F40T2I");
assertEquals(deH32("91IMOR3F40T2I"), binary);
c32
Crockford base 32 uses digits and uppercase
letters, excluding "I", "L", "O", and "U". Encoding is always uppercase, and
decoding is case-insensitive, and additionally accepts hyphens (which don't
affect the output) and substitutes "I", "L", and "O" characters for their
similar-looking numeric counterparts ("1", "1", and "0", respectively).
import { deC32, enC32 } from "@libn/base/c32";
import { assertEquals } from "@std/assert";
const binary = new TextEncoder().encode("Hello :)");
assertEquals(enC32(binary), "91JPRV3F40X2J");
assertEquals(deC32("91JPRV3F40X2J"), binary);
b58
Base58 uses
alphanumeric characters, excluding "0", "I", "O", and "l".
import { deB58, enB58 } from "@libn/base/b58";
import { assertEquals } from "@std/assert";
const binary = new TextEncoder().encode("Hello :)");
assertEquals(enB58(binary), "D7LMXYjUZJQ");
assertEquals(deB58("D7LMXYjUZJQ"), binary);
b64
Base64 uses alphanumeric
characters and "+" and "/", and "=" for padding.
import { deB64, enB64 } from "@libn/base/b64";
import { assertEquals } from "@std/assert";
const binary = new TextEncoder().encode("Hello :)");
assertEquals(enB64(binary), "SGVsbG8gOik=");
assertEquals(deB64("SGVsbG8gOik="), binary);
u64
Base64url uses alphanumeric
characters and "-" and "_". Padding is neither encoded nor decoded.
import { deU64, enU64 } from "@libn/base/u64";
import { assertEquals, assertMatch } from "@std/assert";
const binary = new TextEncoder().encode("Hello :)");
assertEquals(enU64(binary), "SGVsbG8gOik");
assertEquals(deU64("SGVsbG8gOik"), binary);
z85
Z85 uses printable, non-whitespace ASCII
characters, excluding '"', "'", ",", ";", "\", "_", "`", "|", and "~". Unlike
the original, the binary
input length doesn't have to be a multiple of 4, the encoding and decoding
functions add and remove padding automatically.
import { deZ85, enZ85 } from "@libn/base/z85";
import { assertEquals, assertMatch } from "@std/assert";
const binary = new TextEncoder().encode("Hello :)");
assertEquals(enZ85(binary), "nm=QNzY?7&");
assertEquals(deZ85("nm=QNzY?7&"), binary);
a85
Ascii85 uses the first 85 printable,
non-whitespace ASCII characters, as well as "z" to compress sequences of 4 empty
bytes. Unlike the original,
the binary input length doesn't have to be a multiple of 4, the encoding and
decoding functions add and remove padding automatically.
import { deA85, enA85 } from "@libn/base/a85";
import { assertEquals } from "@std/assert";
const binary = new TextEncoder().encode("Hello :)");
assertEquals(enA85(binary), "87cURD]h(i");
assertEquals(deA85("87cURD]h(i"), binary);