Skip to content

Instantly share code, notes, and snippets.

@hh-lohmann
Forked from BobbyRyterski/base62.sh
Created April 11, 2021 13:45
Show Gist options
  • Save hh-lohmann/97a73991214035565c8ce1aeeee4b9cc to your computer and use it in GitHub Desktop.
Save hh-lohmann/97a73991214035565c8ce1aeeee4b9cc 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