Convert Hexadecimal to Decimal in Bash

29/12/2020
Four types of number systems are popular in computer systems. These are Decimal, Binary, Octal and Hexadecimal. The binary system is 2 based and all arithmetic calculations are done by computer in Binary system. It uses only two digits, 0 and 1 for calculation. The number system that we use for general calculation is decimal system which is 10 based. 0 to 9 numbers are used in the decimal system for calculation. The octal number system is 8 based and represented by 0 to 7 digits. The hexadecimal number system is 16 based and it uses 0 to 9 and A to F characters to represents the number. You can easily convert one number to another number system using the bash script. How you can convert Hexadecimal (hex) number to Decimal number in Bash is shown in this tutorial using various examples.

Example-1: Using obase, ibase and bc

One of the simple ways to convert any number system to another number system is to use ibase, obase and bc. Create a bash file named hextodec1.sh and add the following code. According to this example, a hex number will be taken as input and converted into the decimal number based on the value of obase and ibase. Here, obase is set to 10 for converting decimal number, ibase is set to 16 to take the input number as hex number and `bc` command is used for conversion.

#!/bin/bash
echo "Type a hex number"
read hexNum
echo -n "The decimal value of $hexNum="
echo "obase=10; ibase=16; $hexNum" | bc

Output:

Run the script with bash command and give any hexadecimal number as input to find out the decimal value.

$ bash hextodec1.sh

Example-2: Using ibase, command line argument and bc

Create a bash file named hextodec2.sh and add the following code. In this example, the input value has to give in the command line argument, which will be read by $@.  Here, just ibase with 16 value is used to convert hex to the decimal number. 

#!/bin/bash
echo -n "The decimal value of $@="
echo "ibase=16; $@"|bc

Output:

Run the script with bash command, file name and a hexadecimal number as command line argument. Here, FF is given as command line argument which is taken as hex value.

$ bash hextodec2.sh FF

Example-3: using printf method

Another option for converting hex to the decimal number is printf. ‘%d’ format specifier is used in printf method to convert any number to decimal number. Create a bash file named hextodec3.sh and add the following code. According to this script, a hex number will be taken as input and it is used in printf method with %d to print the decimal value.

#!/bin/bash
echo "Type a hex number"
read hexNum
printf "The decimal value of $hexNum=%dn" $((16#$hexNum))

Output:

Run the script with bash command and give any hexadecimal number as input to find out the decimal value.

$ bash hextodec3.sh

Example-4: using double brackets

There is another way to convert hex to the decimal number without using ibase, obase and bc or printf method. You can use double brackets expression with 16 base to convert hex to the decimal number. Create a bash file named hextodec4.sh and add the following code. Here, echo command will take the number as hex and print the output in the decimal number system.

#!/bin/bash
echo "Type a hex number"
read hexNum
echo $(( 16#$hexNum ))

Output:

Run the script with bash command and give any hexadecimal number as input to find out the decimal value.

$ bash hextodec4.sh

Example-5: Converting the list of hexadecimal numbers

Suppose, you have a text file named ‘hexList.txt’ that contains the following list of hex numbers.

HexList.txt
AB05
FF
ABCD
ACCD
BED

Create a bash file named hextodec5.sh and add the following code to convert each hex value of hexList.txt into the decimal value. Here, obase, ibase, and bc are used for conversion. while loop is used to read each hex value from the text file, convert to decimal value and print.

#!/bin/bash
while read number
do
echo -n "The decimal value of $number(Hex)="
echo "obase=10; ibase=16; $number" | bc
done < hexList.txt

Output:

Run the script with bash command. There are five hex values in the text file and the output shows five decimal values after conversion.

$ bash hextodec5.sh

This tutorial shows multiple ways to convert hex to decimal values using the bash script. You can follow any of the ways for your conversion purpose. You can also convert other number systems using the scripts mentioned in this tutorial just by changing the base value.

Sandclock IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, Sandclock IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Bash Parameter Expansion

The parameter is used in bash to store data. Different types of data can be stored in the parameter, such as integer, string,...
29/12/2020

tput, printf and shell expansions: how to create awesome outputs with bash scripts?

1. Why are good outputs so important in bash scripts? There are many, many times when you, as a system administrator, need...
28/12/2020

How to Change Colors on LS in Bash

If you work on the command line interface of Linux most of the time, then changing the color of ls command might be something...
28/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Hướng dẫn chuyển đổi windows server windows evaluation to standard và active windows server 2008 + 2012 + 2016 + 2019
26/10/2021

How to Update Ubuntu Linux
24/10/2021

Squid Proxy Manager cài đặt và quản lý Proxy Squid tự động trên ubuntu
20/10/2021

Hướng dẫn cài đặt Apache CloudStack 4.15.2.0
19/10/2021

Hướng dẫn ký file PDF bằng chữ ký số (chữ ký điện tử) và sửa lỗi mới nhất 2021 foxit reader
19/10/2021