-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathall_your_base.sh
More file actions
27 lines (21 loc) · 797 Bytes
/
Copy pathall_your_base.sh
File metadata and controls
27 lines (21 loc) · 797 Bytes
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
#!/usr/bin/env bash
function die { echo "$1" >&2 ; exit 1; }
function converter {
[[ "$2" =~ - ]] && die "negative value detected"
local origin_base="$1" value=($2) return_base="$3" sum=0
(( origin_base <= 1 || return_base <= 1 )) && die "invalid base"
digits=$(( ${#value[@]} - 1 ))
for number in "${value[@]}"; do
(( number >= origin_base )) && die "digit $number invalid for base $origin_base"
(( sum += number * (origin_base ** digits--) ))
done
while (( sum > 0 )); do
output+=( $(( sum % return_base )) )
(( sum /= return_base ))
done
#tac to reverse the order of the array
output=($(printf '%s\n' "${output[@]}" | tac))
[[ -z "${output[*]}" ]] && output=(0)
echo "${output[@]}"
}
converter "$1" "$2" "$3"