Brief: This guide explores various ways that you can use to convert hexadecimal to decimal values in Linux bash scripting.
In computing, there are four types of numbers and they are Decimal, Binary, Octal, and Hexadecimal.
- The decimal system is used for general calculation and represents the base-10 number system.
- The binary number system is base-2 and the primary language of computer systems. Without it, computers wouldn’t exist. It’s the basis for storing and transmitting digital data and uses two values to perform computations; 0 and 1.
- The octal notation is a base-8 and is represented by digits 0 to 7.
- The hexadecimal notation uses 0 to 9 and A to F characters to represent the number.
In bash scripting, you can convert one number system to another and that is precisely what this guide is all about.
Convert Hexadecimal to Decimal Using echo Command
There are several ways of converting hexadecimal numbers to decimal in bash. The simplest way is running the following echo command where hex-number is the hexadecimal digit.
$ echo $((16#hex-number)
For example, to convert the hexadecimal 75 number to decimal, run the following command on the shell prompt.
$ echo $((16#75))
You should get the following output on the terminal.
Convert Hexadecimal to Decimal Using BC Command
BC (short for basic calculator) is an arbitrary calculator language and compiler that is typically used either as an interactive mathematical shell or a mathematical scripting language.
BC comes installed by default on modern Linux distributions. You can confirm this by running the command:
$ bc bc 1.07.1 Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006, 2008, 2012-2017 Free Software Foundation, Inc. This is free software with ABSOLUTELY NO WARRANTY. For details type `warranty'.
To quit the prompt, type quit and press ENTER.
To convert a hexadecimal value to a decimal using bc, use the following syntax with the ibase value set to 16.
$ echo "ibase=16; hex-number"|bc
For example, to convert the hexadecimal value FF1A, run the following on the shell.
$ echo "ibase=16;FF1A"|bc
Convert Hexadecimal to Decimal Using the printf Command
The bash printf command is used to print formatted text to stdout or on the screen. It is the equivalent of the printf()
function in Java, C, and C++ to mention a few examples.
To convert a hexadecimal value to a decimal one using the printf command, use the following syntax.
$ printf "%d\n" 0xhex-number
For example, to convert the hexadecimal value AADF to a decimal value using the printf shell function, run the following shell command:
$ printf "%d\n" 0xAADF
Conclusion
That was a walkthrough of the different ways you can use to convert hexadecimal to decimal values in a Linux bash shell. Any additional ideas that you might have up your sleeves? Do let us know.
@Pradeep,
Glad that my article helped you to convert hexadecimal to decimal values…
You can also achieve the same results using the wcalc command, which is a command-line calculator created to perform all valid mathematical expressions in a Linux shell…
Hi,
Thanks for sharing the tip…
I managed to convert hexadecimal representation to decimal numbers using the printf command in the bash shell.
@Pradeep,
Keep visiting our site to know more about such Linux command-line tips…