encodeBase32

import { encodeBase32 } from "jsr:@ende/encoding/unstable-base32";

function ยท experimental

function encodeBase32(input: string | Uint8Array_ | ArrayBuffer, options: Base32Options = [UNSUPPORTED]): string

encodeBase32 takes an input source and encodes it into a base32 string. If a Uint8Array<ArrayBuffer> or ArrayBuffer is provided, the underlying source will be detached and reused for the encoding. If you need the input source after providing it to this function, call .slice() to pass in a copy.

input: string | Uint8Array_ | ArrayBuffer

The input source to encode.

options: Base32Options = [UNSUPPORTED]

The options to use for encoding.

Returns

The base32 string representation of the input.

Example: Basic Usage

import { assertEquals } from "@ende/assert";
import {
  encodeBase32,
  type Uint8Array_,
} from "@ende/encoding/unstable-base32";

assertEquals(
  encodeBase32("Hello World", { alphabet: "base32" }),
  "JBSWY3DPEBLW64TMMQ======",
);
assertEquals(
  encodeBase32(
    new TextEncoder().encode("Hello World") as Uint8Array_,
    { alphabet: "base32" },
  ),
  "JBSWY3DPEBLW64TMMQ======",
);

assertEquals(
  encodeBase32("Hello World", { alphabet: "base32hex" }),
  "91IMOR3F41BMUSJCCG======",
);
assertEquals(
  encodeBase32(
    new TextEncoder().encode("Hello World") as Uint8Array_,
    { alphabet: "base32hex" },
  ),
  "91IMOR3F41BMUSJCCG======",
);

assertEquals(
  encodeBase32("Hello World", { alphabet: "base32crockford" }),
  "91JPRV3F41BPYWKCCG======",
);
assertEquals(
  encodeBase32(
    new TextEncoder().encode("Hello World") as Uint8Array_,
    { alphabet: "base32crockford" },
  ),
  "91JPRV3F41BPYWKCCG======",
);

Source