34 lines
719 B
Bash
34 lines
719 B
Bash
|
#!/bin/bash
|
||
|
#
|
||
|
# any2dec.sh <radix> <string_representation_in_that_radix>
|
||
|
#
|
||
|
if [[ $# -lt 2 ]]
|
||
|
then
|
||
|
echo "Usage: $0 <radix> <string_representation_in_that_radix>"
|
||
|
exit
|
||
|
fi
|
||
|
if [[ $1 -lt 2 || $1 -gt 36 ]]
|
||
|
then
|
||
|
echo "The radix is a decimal between 2 and 36"
|
||
|
exit
|
||
|
fi
|
||
|
DIGITS=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
|
||
|
LEGAL_DIGITS=$(expr substr $DIGITS 1 $1)
|
||
|
decimal=0
|
||
|
l=$(expr length $2)
|
||
|
pos=1;
|
||
|
for ((pow=l-1;pow>=0;pow--))
|
||
|
do
|
||
|
digit=$(expr substr $2 pos 1)
|
||
|
digit=&(($(expr index $LEGAL_DIGITS $digit) -1))
|
||
|
if [[ $digit -eq -1 ]]
|
||
|
then
|
||
|
echo "Authorized digits are : $LEGAL_DIGITS"; exit
|
||
|
fi
|
||
|
decimal=$((decimal+digit*$1**pow))
|
||
|
echo "$decimal+$digit*$1**$pow"
|
||
|
pos=$((pos+1))
|
||
|
done
|
||
|
echo "decimal : $decimal"
|
||
|
exit
|