{"id":2660,"date":"2018-11-06T05:42:26","date_gmt":"2018-11-06T05:42:26","guid":{"rendered":"https:\/\/www.appservgrid.com\/paw92\/?p=2660"},"modified":"2018-11-07T14:21:41","modified_gmt":"2018-11-07T14:21:41","slug":"convert-hexadecimal-to-decimal-in-bash","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/11\/06\/convert-hexadecimal-to-decimal-in-bash\/","title":{"rendered":"Convert Hexadecimal to Decimal in Bash"},"content":{"rendered":"<p>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.<\/p>\n<p>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.<\/p>\n<p>#!\/bin\/bash<br \/>\necho &#8220;Type a hex number&#8221;<br \/>\nread hexNum<br \/>\necho -n &#8220;The decimal value of $hexNum=&#8221;<br \/>\necho &#8220;obase=10; ibase=16; $hexNum&#8221; | bc<\/p>\n<p>Output:<\/p>\n<p>Run the script with bash command and give any hexadecimal number as input to find out the decimal value.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/image\/gif;base64,R0lGODdhAQABAPAAAP\/wAAACwAAAAAAQABAEACAkQBADs=\" alt=\"\" width=\"734\" height=\"101\" \/><\/p>\n<h2>Example-2: Using ibase, command line argument and bc<\/h2>\n<p>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.<\/p>\n<p>#!\/bin\/bash<br \/>\necho -n &#8220;The decimal value of $@=&#8221;<br \/>\necho &#8220;ibase=16; $@&#8221;|bc<\/p>\n<p>Output:<\/p>\n<p>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.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/image\/gif;base64,R0lGODdhAQABAPAAAP\/wAAACwAAAAAAQABAEACAkQBADs=\" alt=\"\" width=\"733\" height=\"75\" \/><\/p>\n<h3>Example-3: using printf method<\/h3>\n<p>Another option for converting hex to the decimal number is printf. \u2018%d\u2019 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.<\/p>\n<p>#!\/bin\/bash<br \/>\necho &#8220;Type a hex number&#8221;<br \/>\nread hexNum<br \/>\nprintf &#8220;The decimal value of $hexNum=%dn&#8221; $((16#$hexNum))<\/p>\n<p>Output:<\/p>\n<p>Run the script with bash command and give any hexadecimal number as input to find out the decimal value.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/image\/gif;base64,R0lGODdhAQABAPAAAP\/wAAACwAAAAAAQABAEACAkQBADs=\" alt=\"\" width=\"733\" height=\"99\" \/><\/p>\n<h3>Example-4: using double brackets<\/h3>\n<p>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.<\/p>\n<p>#!\/bin\/bash<br \/>\necho &#8220;Type a hex number&#8221;<br \/>\nread hexNum<br \/>\necho $(( 16#$hexNum ))<\/p>\n<p>Output:<\/p>\n<p>Run the script with bash command and give any hexadecimal number as input to find out the decimal value.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/image\/gif;base64,R0lGODdhAQABAPAAAP\/wAAACwAAAAAAQABAEACAkQBADs=\" alt=\"\" width=\"735\" height=\"102\" \/><\/p>\n<h3>Example-5: Converting the list of hexadecimal numbers<\/h3>\n<p>Suppose, you have a text file named \u2018hexList.txt\u2019 that contains the following list of hex numbers.<\/p>\n<p>HexList.txt<br \/>\nAB05<br \/>\nFF<br \/>\nABCD<br \/>\nACCD<br \/>\nBED<\/p>\n<p>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.<\/p>\n<p>#!\/bin\/bash<br \/>\nwhile read<\/p>\n<p>number<\/p>\n<p>doecho -n &#8220;The decimal value of $number(Hex)=&#8221;<\/p>\n<p>echo &#8220;obase=10; ibase=16; $number&#8221; | bc<br \/>\ndone &lt;<\/p>\n<p>hexList.txt<\/p>\n<p>Output:<\/p>\n<p>Run the script with bash command. There are five hex values in the text file and the output shows five decimal values after conversion.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/image\/gif;base64,R0lGODdhAQABAPAAAP\/wAAACwAAAAAAQABAEACAkQBADs=\" alt=\"\" width=\"734\" height=\"134\" \/><\/p>\n<p>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.<\/p>\n<p><a href=\"https:\/\/linuxhint.com\/convert_hexadecimal_decimal_bash\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/11\/06\/convert-hexadecimal-to-decimal-in-bash\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Convert Hexadecimal to Decimal in Bash&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2660","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/2660","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=2660"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/2660\/revisions"}],"predecessor-version":[{"id":2828,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/2660\/revisions\/2828"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=2660"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=2660"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=2660"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}