Skip to content

Instantly share code, notes, and snippets.

@benjaminion
Created May 18, 2025 17:10
Show Gist options
  • Save benjaminion/b359fb880d9b808d2d7ad31157fd7e40 to your computer and use it in GitHub Desktop.
Save benjaminion/b359fb880d9b808d2d7ad31157fd7e40 to your computer and use it in GitHub Desktop.
Base N (N <= 62) encode an integer
const baseChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
const base = 62;
function baseEncode(n) {
let b = base;
let m = n;
let ret = '';
while (b <= n) {
b = base * (1 + b);
ret = baseChars[m % base] + ret;
m = Math.floor(m / base) - 1;
}
return baseChars[m % base] + ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment