Hex2dec


 * 1) !/bin/bash
 * 2) hex to decimal number converter
 * 3) mutante (s23.org) 11/2006

read -p "Give me a hex number " hex length=${#hex} i=0 result=0 let length-- pos=0 while [ $length -ge 0 ]; do digit=${hex:$length:1}

case "$digit" in A | a) ddigit=10;; B | b) ddigit=11;; C | c) ddigit=12;; D | d) ddigit=13;; E | e) ddigit=14;; F | f) ddigit=15;; [0-9]) ddigit=$digit;; esac factor=`echo "(16^$pos)" | bc` subresult=`echo "$factor*$ddigit" | bc` echo "[$pos]: $digit = $ddigit * (16^$pos) = $ddigit * $factor = $subresult" let result=$result+$subresult let pos++ let length-- done
 * ) echo "Bad input. Only 0-9 and a-f are allowed."; exit 1;;

echo "Result: $hex (hex) is $result (dec)" | boxes exit 0 