Tiny Encodings
A tiny, dependency-free set of fast, constant-time encoders and decoders for Base64 (standard and URL) and Hex, plus endianness helpers for converting typed arrays to and from specific byte orders.
Install
npm i @levischuck/tiny-encodings
pnpm add @levischuck/tiny-encodings
deno add jsr:@levischuck/tiny-encodings
bunx jsr add @levischuck/tiny-encodings
Quick start
quick-start.tsts
import { encodeHex, decodeHex, encodeBase64, decodeBase64, encodeBase64Url, decodeBase64Url } from '@levischuck/tiny-encodings'
const data = new TextEncoder().encode('hello') // Hex const hex = encodeHex(data) // "68656C6C6F" const bytesFromHex = decodeHex(hex) // Uint8Array([104,101,108,108,111]) // Base64 (RFC 4648) const b64 = encodeBase64(data) // "aGVsbG8=" const bytesFromB64 = decodeBase64(b64) // Uint8Array([...]) // Base64 URL (RFC 4648 URL/Filename Safe) const b64u = encodeBase64Url(data) // "aGVsbG8" const bytesFromB64u = decodeBase64Url(b64u) // Uint8Array([...])
Hex
encodeHex(array: BufferType): string
decodeHex(text: string): Uint8Array
hex.tsts
import { encodeHex, decodeHex } from '@levischuck/tiny-encodings'
const data = new Uint8Array([0, 1, 2, 253, 254, 255]) const hex = encodeHex(data) // "000102FDFEFF" const roundTrip = decodeHex(hex) // Uint8Array([0,1,2,253,254,255])
Hex decoding rejects whitespace or separators and requires an even-length, valid hex string.
Base64
encodeBase64(array: BufferType): string
decodeBase64(text: string): Uint8Array
base64.tsts
import { encodeBase64, decodeBase64 } from '@levischuck/tiny-encodings'
const bytes = new TextEncoder().encode('hello') const b64 = encodeBase64(bytes) // "aGVsbG8=" (RFC 4648 with padding) const back = decodeBase64(b64) // Uint8Array([...])
Base64 URL
encodeBase64Url(array: BufferType): string
decodeBase64Url(text: string): Uint8Array
base64url.tsts
import { encodeBase64Url, decodeBase64Url } from '@levischuck/tiny-encodings'
const bytes = new TextEncoder().encode('hello') const b64u = encodeBase64Url(bytes) // "aGVsbG8" (URL-safe, no padding) const back = decodeBase64Url(b64u) // Uint8Array([...])