diff options
Diffstat (limited to 'bin/radix')
-rwxr-xr-x | bin/radix | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/bin/radix b/bin/radix new file mode 100755 index 0000000..670107d --- /dev/null +++ b/bin/radix @@ -0,0 +1,39 @@ +#!/bin/bash + +# bc -q <<<"obase=$1;$2" + +set -eu + +if [[ $# -eq 0 ]] || [[ $# -gt 2 ]] +then + cat >&2 <<-HERE + usage: $0 radix decimal + HERE + exit +fi + +declare -i radix=$1 +declare -i decimal=$2 + +[[ radix -gt 0 ]] || exit 1 + +if [[ $decimal -lt 0 ]] +then + printf -- - + decimal=-$decimal +fi + +if [[ $radix -eq 12 ]] +then digits=({0..9} ↊ ↋) +else digits=({0..9} {a..z}) +fi +number='' + +while [[ $decimal -gt 0 ]] +do + digit=$((decimal%radix)) + number=${digits[$digit]}$number +# decimal=$((decimal-(digit*radix))) + decimal=$((decimal/radix)) +done +echo "$number" |