Linux / UNIX: Convert Hexadecimal to Decimal Number

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

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

26 comments… add one
  • Huckle Jan 18, 2013 @ 22:38

    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
    
  • hari Apr 2, 2013 @ 12:45

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

  • Rombus May 15, 2013 @ 0:29

    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

  • mickael Oct 1, 2013 @ 7:02

    I use python.
    python -c “print 0x100”
    python -c “print hex(256)”

  • Melchior FRANZ Jun 1, 2015 @ 9:19

    With a sufficiently recent bash: echo $((16#fff))

  • Swap Dec 23, 2016 @ 12:29

    echo $((0x55aa))

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.