Public
Edited
Sep 19, 2024
Insert cell
Insert cell
Insert cell
Insert cell
btoa(input) // it's longer
Insert cell
Insert cell
input.toString(16) // basically the same size
Insert cell
input.toString(36) // a little bit shorter
Insert cell
input.toString(64) // lame!
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
let bytes = new TextEncoder().encode(String(input))
const binString = Array.from(bytes, (byte) =>
String.fromCodePoint(byte),
).join("");
return btoa(binString) // oh, there it is
}
Insert cell
Insert cell
customBase64(input) // snippet from the internet
Insert cell
Insert cell
customBase64 = function(num) {
const BASE_BITS = 6
const alphabet = "-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"

let log = Math.log2(num)
if (Math.pow(2, Math.round(log)) === num) {
log++
}
let length = Math.max(1, Math.ceil(log / BASE_BITS))

let chars = new Array(length)
let x = chars.length - 1
while (num > 0) {
chars[x--] = alphabet[num % BASE]
num = Math.floor(num / BASE)
}
while (x >= 0) {
chars[x--] = alphabet[0]
}
return chars.join('')
}

Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
BASE = 64
Insert cell
BASE_BITS = 6
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more