# encoding: ascii
# api: bash
# title: hex2dec
# description: Old bash script…
# version: 0.1
# author: greg zakharov
# license: CC0
# x-poshcode-id: 4562
# x-archived: 2013-11-01T01:43:17
# x-published: 2013-10-26T16:45:00
#
#
#!/bin/bash
err="\033[1;31m"
res="\033[1;36m"
end="\033[0m"
if [ $# -eq 1 ]; then
n=$1
case $n in
*[[:xdigit:]])
if [[ ${n:0:2} =~ 0[xX] ]]; then
echo -e $res$n' = '$(($n))$end
elif [[ ${n:0:1} =~ [xX] ]]; then
echo -e $res'0'$n' = '$((0$n))$end
elif [[ ${n:0:1} =~ [[:digit:]] && ! $n =~ [[:alpha:]] ]]; then
printf $res$n' = 0x%X\n'$end $n
elif [[ ${n:0:1} =~ [[:xdigit:]] ]]; then
echo -e $res'0x'$n' = '$((0x$n))$end
else
echo -e $err'=>err'$end
fi;;
*)
echo -e $err'=>err'$end;;
esac
else
echo -e $err'=>err'$end
fi