Linux / UNIX: Convert Hexadecimal to Decimal Number

by on February 6, 2009 · 24 comments· LAST UPDATED November 23, 2010

in , ,

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 the shell prompt to convert hex to decimal number or vice versa.

bc: Hexadecimal or Binary Conversion

To convert to decimal, set ibase to 16, enter:

echo "ibase=16; hex-number"|bc
echo "ibase=16; FFF"|bc

Sample output:

4095

To convert to hexadecimal, set obase to 16, enter:

echo "obase=16; decimal-number"|bc
echo "obase=16; 10"|bc

Sample output:

A

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 "ibase=16; $@"|bc
}
d2h(){
  echo "obase=16; $@"|bc
}

The above two functions can be used from the command line as follows:
$ h2d 100
$ d2h AC

Base conversion using printf shell builtin

You can also use printf a shell builtin as well as /usr/bin/printf. To convert decimal to hex, you'd type:

printf "%x\n" 4095

Sample outputs:

fff

To convert hex to decimal, you'd type:

printf "%d\n" 0xfff

Sample outputs:

4095

You can save result to a shell variable and print it using printf or echo command:

output=$(printf "%d\n" 0xfff)
echo "${output}"
printf "%d\n" $output
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 24 comments… read them below or add one }

1 Amir Watad February 7, 2009 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.

Reply

2 Loïc Pefferkorn February 7, 2009 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 !

Reply

3 sanjeev February 7, 2009 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.

Reply

4 nixCraft February 7, 2009 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.

Reply

5 tim February 7, 2009 at 1:11 pm

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

Reply

6 Peck February 7, 2009 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

Reply

7 Maqbool Patel February 8, 2009 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.

Reply

8 Mockey Chen February 9, 2009 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.

Reply

9 asdf February 15, 2009 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.

Reply

10 mhymn February 25, 2009 at 11:31 am

aptitude install gbase && man gbase
:)

Reply

11 PK March 16, 2009 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))

Reply

12 PK March 16, 2009 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

Reply

13 drpyro July 4, 2009 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

Reply

14 Justin Anonymous September 23, 2009 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).

Reply

15 adithya kiran February 23, 2011 at 6:54 am

Hi,

You can use the following command to convert from any to any base.. for example binary to dec and dec to binary

# perl -e ‘printf “%b\n” 10′
1010
# perl -e ‘printf “%d\n” 0b1010′
10

Reply

16 sovan November 25, 2011 at 6:09 pm

how the conversion will possible when a user will give a number on his choice or from command line argument.

Reply

17 andreas July 20, 2012 at 12:33 pm

to JA & drpyro:
yes, interesting, seems like once you set “ibase=2″, it treats the number in the subsequent “obase=10″ in binary and interprets it as a decimal “2″ and accordingly instructs to output in binary form, therefore producing the initial binary input number.
But you needn’t fuss over converting the decimal “10″ to binary “1010″ just to let bc interpret obase correctly – just specify the “obase=10″ before specifying the ibase:

echo “obase=10; ibase=2; 111101101″ | bc

also, specifying “obase=10″ after the binary input number works:

echo “ibase=2; 111101101 ; obase=10;” | bc

outputing in the decimal base is the default bc behaviour so can be dropped:

echo “ibase=2; 111101101″ | bc

Reply

18 noname July 28, 2012 at 11:57 am

Your perl-example is a “,” after the format-part missing:
# perl -e ‘printf “%b\n”, 10′
1010
# perl -e ‘printf “%d\n”, 0b1010′
10

Reply

19 Adithya Kiran Gangu July 29, 2012 at 5:24 am

Thanks dude for the minor correction

Reply

20 Harry Langenbacher November 18, 2012 at 8:29 pm
#!/bin/bash
#hextodec
#convert any number of hex numbers to decimal
#use "silent" as an argument to get briefer output for subsequent arguments. Example
#hextodec abc silent fff
if [[ ! $1 ]]
 then
  echo convert any number of hex numbers to decimal
  echo use \"silent\" as an argument to get briefer output for subsequent arguments. Example
  echo $0 abc silent fff
fi
for hex in $*
 do
  if [[ "$hex" == silent ]]
   then
    silent=1
    continue
   else
    if [[ "${hex/[^0-9A-Fa-f]}" != "$hex" ]]
     then
      echo "\"$hex\" is not a hex number - just use hex digits"
      continue
    fi
  fi
  let value=0x$hex
  if (( ! silent ))
   then
    echo -n "hex 0x$hex = decimal "
  fi
  echo $((value))
done

————————————————————————–

#!/bin/bash
#convert any number of binary numbers to hex
#use "silent" as an argument to get briefer output for subsequent arguments. Example
#bintohex 11111 silent 111111
if [[ ! $1 ]]
 then
  echo convert any number of binary numbers to hex
  echo use \"silent\" as an argument to get briefer output for subsequent arguments. Example
  echo $0 11111 silent 111111
fi
for binary in $*
 do
  if [[ "$binary" == silent ]]
   then
    silent=1
    continue
   else
    if [[ "${binary/[^01]}" != "$binary" ]]
     then
      echo "\"$binary\" is not a binary number - just use 0's and 1's"
      continue
    fi
  fi
  let value=2#$binary
  if (( ! silent ))
   then
    echo -n "binary 2#$binary = hexadecimal 0x"
  fi
  printf "%X\n" $value
done

Reply

21 Huckle January 18, 2013 at 10:38 pm

A major problem with the people suggesting printf is the lack of precision compared to bc. Consider the following

$ printf "%d\n"  0xD9416E53C29E8257A374A02F2862052032DDA5AE7ECCEAFF3553491E5D1DA02068A7888DF6ED573A73B7156ACEDF1A8A5339CC267D731C9B09D6FDBF422F9FAC05B564568B7CAAF613D7D6161BAEFC50F228CA81D862BDCF0D17833DF87C5B9869AFDD9A2E757BB14FD58EC62D4BE64AB98C8440318EE624700E10D645DFBC3B
bash: printf: warning: 0xD9416E53C29E8257A374A02F2862052032DDA5AE7ECCEAFF3553491E5D1DA02068A7888DF6ED573A73B7156ACEDF1A8A5339CC267D731C9B09D6FDBF422F9FAC05B564568B7CAAF613D7D6161BAEFC50F228CA81D862BDCF0D17833DF87C5B9869AFDD9A2E757BB14FD58EC62D4BE64AB98C8440318EE624700E10D645DFBC3B: Numerical result out of range
9223372036854775807

vs

$ echo "ibase=16; D9416E53C29E8257A374A02F2862052032DDA5AE7ECCEAFF3553491E5D1DA02068A7888DF6ED573A73B7156ACEDF1A8A5339CC267D731C9B09D6FDBF422F9FAC05B564568B7CAAF613D7D6161BAEFC50F228CA81D862BDCF0D17833DF87C5B9869AFDD9A2E757BB14FD58EC62D4BE64AB98C8440318EE624700E10D645DFBC3B" | bc
15256206333236909607074123047335714924824051962962261484438970616724\
16065731202741278668636933358898393872253458882879611872300713906719\
21426741919669350334837200474802435375507601309711393907894616987077\
89788816453030088048266333363441906590295054112097178868191240854462\
8193927787051199707221470516418165819

Reply

22 hari April 2, 2013 at 12:45 pm

Where is the program yaar….i know the logic ,but most of the guys fail to implement the logic.;…

Reply

23 Rombus May 15, 2013 at 12:29 am

There is a little gui app for doing this. Anyone knows it’s name? I’ve installed it but I can’t remember it’s name so I cant launch it lol

Reply

24 mickael October 1, 2013 at 7:02 am

I use python.
python -c “print 0×100″
python -c “print hex(256)”

Reply

Leave a Comment

Tagged as: , , , , , , , , , , , , , , , , , , , , , , , , ,

Previous Faq:

Next Faq: