Linux / UNIX: Convert Hexadecimal to Decimal Number

by Vivek Gite · 14 comments

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:

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 14 comments… read them below or add one }

1 Amir Watad 02.07.09 at 3:03 am

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.

2 Loïc Pefferkorn 02.07.09 at 11:35 am

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 !

3 sanjeev 02.07.09 at 11:38 am

I tried this but i am not able to do this.
I edited “~/.bashrc ” file copy and pasted the above code.

4 Vivek Gite 02.07.09 at 12:04 pm

@Sanjeev,

You need to logout and login again so that functions get loaded.

@Loïc Pefferkorn

Thanks for sharing wcalc tool.

5 tim 02.07.09 at 1:11 pm

You can also source your .bashrc file:
. ~/.bashrc

6 Peck 02.07.09 at 4:24 pm

There’s an error on d2h. It’s ibase=16 to change input format to hex.
This gives the right value AC -> 172

7 Maqbool Patel 02.08.09 at 5:17 am

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.

8 Mockey Chen 02.09.09 at 2:10 am

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.

9 asdf 02.15.09 at 4:21 pm

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.

10 mhymn 02.25.09 at 11:31 am

aptitude install gbase && man gbase
:)

11 PK 03.16.09 at 6:42 am

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))

12 PK 03.16.09 at 6:48 am

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

13 drpyro 07.04.09 at 1:49 am

this command fail to give me the result on base 10, im i writing it the wrong way ?
echo "ibase=2;obase=10;111101101" | bc

when i omit obase i have the right answer exp: echo "ibase=2;111101101" | bc which is :493
I’m using bc 1.06.94

14 Justin Anonymous 09.23.09 at 6:00 pm

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" | bc

Get it? The obase number has to be in binary (1010b = 10 decimal).

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Previous FAQ:

Next FAQ:

nixCraft FAQ PDF Collection Now Available To All