summaryrefslogtreecommitdiff
path: root/bin/radix
blob: 670107dc5e768ad116152bcf03633b31d8c8fd47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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"