- Convert Hexadecimal to Decimal in Bash
- Example-2: Using ibase, command line argument and bc
- Example-3: using printf method
- Example-4: using double brackets
- Example-5: Converting the list of hexadecimal numbers
- About the author
- Fahmida Yesmin
- Linux / UNIX: Convert Hexadecimal to Decimal Number
- bc – An arbitrary precision calculator language
- bc: Hexadecimal or Binary Conversion
- Base conversion using printf shell builtin
- Can I convert hex to decimal using the calculator app?
- 5 Answers 5
Convert Hexadecimal to Decimal in Bash
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.
Output:
Run the script with bash command and give any hexadecimal number as input to find out the decimal value.
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 [email protected] Here, just ibase with 16 value is used to convert hex to the decimal number.
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.
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.
Output:
Run the script with bash command and give any hexadecimal number as input to find out the decimal value.
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.
Output:
Run the script with bash command and give any hexadecimal number as input to find out the decimal value.
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.
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.
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.
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.
About the author
Fahmida Yesmin
I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.
Источник
Linux / UNIX: Convert Hexadecimal to Decimal Number
H ow 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:
To convert to hexadecimal, set obase to 16, enter:
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
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:
To convert hex to decimal, you’d type:
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
You can save result to a shell variable and print it using printf or echo command:
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Category | List of Unix and Linux commands |
---|---|
Documentation | help • mandb • man • pinfo |
Disk space analyzers | df • duf • ncdu • pydf |
File Management | cat • cp • less • mkdir • more • tree |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Linux Desktop Apps | Skype • Spotify • VLC 3 |
Modern utilities | bat • exa |
Network Utilities | NetHogs • 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 • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop |
Searching | ag • grep • whereis • which |
Shell builtins | compgen • echo • printf |
Text processing | cut • rev |
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 |
Comments on this entry are closed.
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.
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.
You need to logout and login again so that functions get loaded.
Thanks for sharing wcalc tool.
You can also source your .bashrc file:
.
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).
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:
also, specifying “obase=10” after the binary input number works:
echo “ibase=2; 111101101 ; obase=10;” | bc
outputing in the decimal base is the default bc behaviour so can be dropped:
Your perl-example is a “,” after the format-part missing:
# perl -e ‘printf “%b\n”, 10′
1010
# perl -e ‘printf “%d\n”, 0b1010′
10
Источник
Can I convert hex to decimal using the calculator app?
How can we convert a hexadecimal value into decimal by using calculator (I know by using general formula we can do ,but is there any short key or button to do this)?
(I tried but didn’t found any option )
5 Answers 5
You can use Ubuntu’s default calculator in programming mode.
Open the dash and search for Calculator, then select : Mode > Programming Mode.
Enter the value to convert, then press equal =. The entered value will get bolded. Then you may select the target Base from the drop-box to have the value converted.
And if you want something you can do from the command line, you can use trusty old bc
When I need to convert to hex from command line, I do this:
and when I need to convert from hex, it get simpler:
Well, I don’t do it often but, when I do, I just use Galculator. Why do I use Galculator? It is fast, light, and feature rich. It does everything I need it to do, these days.
If you’re looking to convert then simply enter the information and then click on the appropriate button (DEC HEX OCT BIN) and it converts it for you all nice and easy like. Most of all, it’s pretty small and easy to work with — I like small and simple and I also like a GUI for some things.
If you want to install it then sudo apt-get install galculator and follow the prompts.
Источник