Base36 String/Buffer Converter

1.0.0 · active · verified Sun Apr 19

The `b36` package provides a minimalist utility for converting between Node.js `Buffer` objects and Base36 encoded strings. Currently at version 1.0.0, this library offers a slightly more compact encoding scheme compared to hexadecimal, making it suitable for contexts where shorter, alphanumeric identifiers are beneficial, such as unique IDs in URLs or hostnames. Its release cadence is likely stable and infrequent, as it addresses a specific, well-defined encoding problem with a mature solution. Key differentiators include its simplicity, small footprint, and direct focus on Base36, providing a straightforward alternative to more complex encoding libraries when only Base36 is required, especially beneficial in Node.js environments.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to encode a random Node.js `Buffer` into a Base36 string and then decode it back, verifying the integrity of the transformation.

const { encode, decode } = require('b36');
const crypto = require('crypto'); // Node.js built-in

async function runBase36Example() {
  console.log("--- b36 Encoding and Decoding Example ---");

  // 1. Generate a random 32-byte buffer for encoding
  const originalBuffer = crypto.randomBytes(32);
  console.log('Original Buffer (hex):', originalBuffer.toString('hex'));

  // 2. Encode the buffer to a Base36 string
  const base36String = encode(originalBuffer);
  console.log('Encoded Base36 string:', base36String);

  // 3. Decode the Base36 string back to a buffer
  const decodedBuffer = decode(base36String);
  console.log('Decoded Buffer (hex):', decodedBuffer.toString('hex'));

  // 4. Verify that the decoded buffer precisely matches the original
  const areEqual = originalBuffer.equals(decodedBuffer);
  console.log('Original and Decoded buffers are equal:', areEqual);

  if (!areEqual) {
    console.error("Verification failed: Buffers do not match after encode/decode cycle!");
  }
}

runBase36Example();

view raw JSON →