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
🐧 26 comments so far... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
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 $((0x100))
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" | 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
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).
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
how the conversion will possible when a user will give a number on his choice or from command line argument.
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:
Your perl-example is a “,” after the format-part missing:
# perl -e ‘printf “%b\n”, 10′
1010
# perl -e ‘printf “%d\n”, 0b1010′
10
Thanks dude for the minor correction
————————————————————————–
A major problem with the people suggesting printf is the lack of precision compared to bc. Consider the following
vs
Where is the program yaar….i know the logic ,but most of the guys fail to implement the logic.;…
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
I use python.
python -c “print 0x100”
python -c “print hex(256)”
With a sufficiently recent bash: echo $((16#fff))
echo $((0x55aa))