Sunday, December 16, 2012

Shell script to convert HEX to decimal number

This is a small script to convert hex number to decimal numbers. Here we can use different machination to do this. One is to use inbuilt (()) braces to do this. Hexadecimal numbers have base as 16. So if I consider hex value of 13, it will be equal to 1*16+3=19 decimal value.

Below we will see number of ways to do this conversion using .

Method1: Use (()) brace expatiation. 

#!/bin/bash
read -p "Please enter HEX number: " HEX1
echo "The decimal value of $HEX1 is $((16#$HEX1))"

Save above file as hex2dec.sh

Change permissions to this script now and execute as follows

chmod +x hex2dec.sh

./hex2dec.sh

Method2: Using bc command

#!/bin/bash
read -p "Please enter HEX number: " HEX1

echo "ibase=16; $HEX1" | bc


Save above file as hex2dec.sh

Change permissions to this script now and execute as follows

chmod +x hex2dec.sh

./hex2dec.sh



0 comments:

Post a Comment