summaryrefslogtreecommitdiff
path: root/bin/radix
diff options
context:
space:
mode:
authorDavid Wührer <def@gmx.at>2024-05-18 21:04:34 +0200
committerDavid Wührer <def@gmx.at>2024-05-18 21:04:34 +0200
commit77cfffee6177fb0421fa0fce15d6a1875bde4816 (patch)
tree251428cdd482ea50cfb019dbd3c3bafd79d2fd14 /bin/radix
parent75b23ce1ff0daab363725a2a5755ea80bbeaa68e (diff)
manuals, sum-down, radix
Diffstat (limited to 'bin/radix')
-rwxr-xr-xbin/radix39
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"