@std/encoding
Overview Jump to heading
Utilities for encoding and decoding common formats like hex, base64, and varint.
Basic Usage
import { encodeBase64, decodeBase64 } from "@std/encoding";
import { assertEquals } from "@std/assert";
const foobar = new TextEncoder().encode("foobar");
assertEquals(encodeBase64(foobar), "Zm9vYmFy");
assertEquals(decodeBase64("Zm9vYmFy"), foobar);
Various Encoding Formats
import {
encodeHex,
encodeBase32,
encodeBase58,
encodeBase64,
encodeAscii85,
decodeHex,
decodeBase32,
decodeBase58,
decodeBase64,
decodeAscii85,
} from "@std/encoding";
import { assertEquals } from "@std/assert";
// Many different encodings for different character sets
assertEquals(encodeHex("Hello world!"), "48656c6c6f20776f726c6421");
assertEquals(encodeBase32("Hello world!"), "JBSWY3DPEB3W64TMMQQQ====");
assertEquals(encodeBase58("Hello world!"), "2NEpo7TZRhna7vSvL");
assertEquals(encodeBase64("Hello world!"), "SGVsbG8gd29ybGQh");
assertEquals(encodeAscii85("Hello world!"), "87cURD]j7BEbo80");
// Decoding
assertEquals(new TextDecoder().decode(decodeHex("48656c6c6f20776f726c6421")), "Hello world!");
assertEquals(new TextDecoder().decode(decodeBase32("JBSWY3DPEB3W64TMMQQQ====")), "Hello world!");
assertEquals(new TextDecoder().decode(decodeBase58("2NEpo7TZRhna7vSvL")), "Hello world!");
assertEquals(new TextDecoder().decode(decodeBase64("SGVsbG8gd29ybGQh")), "Hello world!");
assertEquals(new TextDecoder().decode(decodeAscii85("87cURD]j7BEbo80")), "Hello world!");
URL-Safe Base64
import { encodeBase64, encodeBase64Url } from "@std/encoding";
import { assertEquals } from "@std/assert";
assertEquals(encodeBase64("ice creams"), "aWNlIGNyZWFtcw=="); // Not url-safe because of `=`
assertEquals(encodeBase64Url("ice creams"), "aWNlIGNyZWFtcw"); // URL-safe!
// Base64Url replaces + with - and / with _
assertEquals(encodeBase64("subjects?"), "c3ViamVjdHM/"); // slash is not URL-safe
assertEquals(encodeBase64Url("subjects?"), "c3ViamVjdHM_"); // _ is URL-safe
Binary Data Encoding
import { encodeHex, encodeBase64 } from "@std/encoding";
import { assertEquals } from "@std/assert";
// Working with binary data
const binaryData = new Uint8Array([0xDE, 0xAD, 0xBE, 0xEF]);
assertEquals(encodeHex(binaryData), "deadbeef");
assertEquals(encodeBase64(binaryData), "3q2+7w==");
Varint Encoding
Learn more from the protobuf Varint encoding docs.
import { encodeVarint, decodeVarint } from "@std/encoding";
import { assertEquals } from "@std/assert";
// Varint encoding support
assertEquals(encodeVarint(9601n), [new Uint8Array([129, 75]), 2]);
// Decode a varint
const bytes = new Uint8Array([129, 75]);
assertEquals(decodeVarint(bytes), [9601n, 2]);
Add to your project Jump to heading
deno add jsr:@std/encoding
See all symbols in @std/encoding on
What is encoding? Jump to heading
Encoding is the process of converting data from one format to another, often to make it suitable for transmission or storage. Common encoding formats include Base64, Hex, and Varint.
Why use @std/encoding? Jump to heading
This package provides simple, reliable functions for encoding and decoding data in various formats. You will use encoding for:
- Interop: exchange binary data in human/transmissible forms (URLs, JSON, command line, logs).
- Safety: decode helpers throw on malformed inputs (e.g., hex) rather than silently truncating.
- Portability: single API surface across runtimes.
- Control: choose URL‑safe Base64 to avoid +, /, = in URLs.
Examples Jump to heading
import {
decodeBase64Url,
decodeHex,
encodeBase64Url,
encodeHex,
} from "@std/encoding";
// Strings <-> bytes
const bytes = new TextEncoder().encode("hello");
const text = new TextDecoder().decode(bytes);
// URL-safe tokens
const token = encodeBase64Url(bytes); // aGVsbG8
const roundtrip = new TextDecoder().decode(decodeBase64Url(token));
// Hex utilities
const hex = encodeHex(bytes); // 68656c6c6f
const fromHex = decodeHex(hex);
// Other supported encodings
import {
decodeAscii85,
decodeBase32,
decodeBase58,
decodeBase64,
encodeAscii85,
encodeBase32,
encodeBase58,
encodeBase64,
} from "@std/encoding";
encodeBase64("Hello world!"); // "SGVsbG8gd29ybGQh"
encodeBase32("Hello world!"); // "JBSWY3DPEB3W64TMMQQQ===="
encodeBase58("Hello world!"); // "2NEpo7TZRhna7vSvL"
encodeAscii85("Hello world!"); // "87cURD]j7BEbo80"
new TextDecoder().decode(decodeBase64("SGVsbG8gd29ybGQh")); // "Hello world!"
// Varint (compact integer encoding)
import { decodeVarint, encodeVarint } from "@std/encoding";
const [buf, end] = encodeVarint(9601n);
const [num, next] = decodeVarint(buf);
Tips Jump to heading
- All encoders/decoders work with
Uint8Array
. UseTextEncoder
/TextDecoder
for string ↔ bytes. - Prefer Base64URL for tokens/query params; standard Base64 may include characters that need escaping.
- Hex helpers return lowercase by default.
- Varint encodes integers compactly; it’s not for arbitrary binary payloads.