Skip to content

Instantly share code, notes, and snippets.

@BobbyRyterski
Last active January 18, 2023 07:36
Show Gist options
  • Save BobbyRyterski/7a511b96ece47655b17d to your computer and use it in GitHub Desktop.
Save BobbyRyterski/7a511b96ece47655b17d to your computer and use it in GitHub Desktop.
BASH base 62 encode / decode
#!/usr/bin/env bash
# https://gist.github.com/BobbyRyterski/7a511b96ece47655b17d
# based on http://stackoverflow.com/q/14471692
readonly BASE62=($(echo {0..9} {a..z} {A..Z}))
base62_encode() {
local out=
for i in $(bc <<< "obase=62; $1"); do
out="${out}${BASE62[$((10#$i))]}"
done
echo $out
}
base62_decode() {
echo $((62#$1))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment