Created
May 18, 2025 17:10
-
-
Save benjaminion/b359fb880d9b808d2d7ad31157fd7e40 to your computer and use it in GitHub Desktop.
Base N (N <= 62) encode an integer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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