From 77cfffee6177fb0421fa0fce15d6a1875bde4816 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20W=C3=BChrer?= Date: Sat, 18 May 2024 21:04:34 +0200 Subject: manuals, sum-down, radix --- bin/radix | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100755 bin/radix (limited to 'bin/radix') 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" -- cgit v1.2.3