Sunday 26 July 2020

Shell script to convert a decimal number to hexadecimal number



PROGRAM

echo "Enter a decimal number"
read n
num=$n
while test $n -gt 0
do
  d=`expr $n % 16`
  case $d in
      10) d='A' ;;
      11) d='B' ;;
      12) d='C' ;;
      13) d='D' ;;
      14) d='E' ;;
      15) d='F' ;;
  esac
  res=$res$d
  n=`expr $n / 16`
done
cnt=`echo $res|wc -c`
while test $cnt -gt 0
do
  c=`echo $res|cut -c $cnt`
  hex=$hex$c
  cnt=`expr $cnt - 1`
done
echo "($num) decimal = ($hex) hexadecimal"



OUTPUT
Enter a decimal number
10456
(10456) decimal = (28D8) hexadecimal

No comments:

Post a Comment