How do I convert hex number to decimal number using a shell script under UNIX / Linux operating systems?
Hexadecimal (hex) is a numeral system with a radix, or base, of 16. It uses sixteen distinct symbols, most often the symbols 0–9 to represent values zero to nine, and A, B, C, D, E, F (or a through f) to represent values ten to fifteen.
bc - An arbitrary precision calculator language
There is no need to write a shell script. You can simply use the following syntax at a shell prompt to convert hex to decimal number or vice versa:
echo "obase=16; hex-number"|bc echo "obase=16; 100"|bc
Sample output:
64
echo "obase=10; decimal-number"|bc echo "obase=10; AC"|bc
Sample output:
99
ibase and obase define the conversion base for input and output numbers under bc. The default for both input and output is base 10. Add following function to your ~/.bashrc:
h2d(){ echo "obase=16; $@"|bc } d2h(){ echo "obase=10; $@"|bc }
The above two functions can be used from the command line as follows:
$ h2d 100
$ d2h AC
Featured Articles:
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- My 10 UNIX Command Line Mistakes
- 10 Greatest Open Source Software Of 2009
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- Top 20 OpenSSH Server Best Security Practices
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Linux Video Editor Software
- Email FAQ to a friend
- Download PDF version
- Printable version
- Comment RSS feed
- Last Updated: 02/6/09



{ 14 comments… read them below or add one }
Hi.
A great post. Thank you for sharing.
BTW, a small correction, I think you swapped the function names h2d/d2h by mistake.
Will surely write about this in my blog. will give you the credits of course.
Thanks again.
Hello,
Thank you for the tip.
wcalc is also a handy way to do such convertions, and I found it easier to use :
To convert from hex to decimal :
$ echo “0xff”|wcalc -d
= 255
To convert from decimal to hex:
echo “255″|wcalc -h
= 0xff
Binary and octal bases are also handled., and there is a _lot_ of others options !
I tried this but i am not able to do this.
I edited “~/.bashrc ” file copy and pasted the above code.
@Sanjeev,
You need to logout and login again so that functions get loaded.
@Loïc Pefferkorn
Thanks for sharing wcalc tool.
You can also source your .bashrc file:
. ~/.bashrc
There’s an error on d2h. It’s ibase=16 to change input format to hex.
This gives the right value AC -> 172
Thanks, these days I am working on some byte level code reading file offsets and I was using a calculator to do this conversion. Never thought of this. Saved me lots of time. Especially the trick of adding to the .bashrc file, neat.
How about just using printf, for example:
convert dec to hex:
$ printf “%x” 100
convert hex to dec:
$ printf “%d” 0xf4
simple and easy way.
0xac != 99, which one should see on a first glance or at least manually check before posting.
bc defaulting to decimal for input AND output saved you in your first example.
Finally clobbering and depending on the .bashrc when there is printf on every unix is unnecessary.
aptitude install gbase && man gbase
:)
I agrre with Mockey Chen.
convert dec to hex:
$ printf “%x” 100
convert hex to dec:
$ printf “%d” 0xf4
Or simply do in bash (for hex to dec. conversion):
echo $((0×100))
One more way I know.
from dec. to hex. conversion:
$echo ‘ibase=10; obase=16; 1237184449′ | bc
49BDEFC1
from hex. to dec. conversion: (here you specify obase in hex notation ( A=10 )
$echo ‘ibase=16; obase=A; 49BDEFC1′ | bc
1237184449
this command fail to give me the result on base 10, im i writing it the wrong way ?
echo "ibase=2;obase=10;111101101" | bcwhen i omit obase i have the right answer exp:
echo "ibase=2;111101101" | bcwhich is :493I’m using bc 1.06.94
As others have mentioned, there is so much wrong with this article. Someone needs to update it.
drpyro: what is not mentioned is once you set ibase, all numbers after are treated in that base. For your code, it should look like this:
echo "ibase=2; obase=1010; 111101101" | bcGet it? The obase number has to be in binary (1010b = 10 decimal).