Can someone help me in telling how to convert a hexadecimal number to decimal number in a shell script? E.g. I want to convert the hexadecimal number bfca3000
to decimal using a shell script. I basically want the difference of two hexadecimal numbers. My code is:
var3=`echo "ibase=16; $var1" | bc`
var4=`echo "ibase=16; $var2" | bc`
var5=$(($var4-$var3)) # [Line 48]
Upon executing ,I am getting the error as:
Line 48: -: syntax error: operand expected (error token is "-")
One more way to do it using the shell (bash or ksh, doesn't work with dash):
Dealing with a very lightweight embedded version of busybox on Linux means many of the traditional commands are not available (bc, printf, dc, perl, python)
Credit to Peter Leung for this solution.
The error as reported appears when the variables are null (or empty):
That could happen because the value given to bc was incorrect. That might well be that bc needs UPPERcase values. It needs
BFCA3000
, notbfca3000
. That is easily fixed in bash, just use the^^
expansion:That will change the script to this:
But there is no need to use bc [1], as bash could perform the translation and substraction directly:
[1]Note: I am assuming the values could be represented in 64 bit math, as the difference was calculated in bash in your original script. Bash is limited to integers less than ((2**63)-1) if compiled in 64 bits. That will be the only difference with bc which does not have such limit.
In dash and other shells, you can use
to convert a hexadecimal number to decimal. This is not bash, or ksh, specific.
To convert hex2dec, these are many ways to do it in a shell or a script :
With bash (beware of the white spaces):
with bc:
with perl:
with printf :
with python:
with ruby:
with node.js:
with rhino:
with groovy:
Various tools are available to you from within a shell. Sputnick has given you an excellent overview of your options, based on your initial question. He definitely deserves votes for the time he spent giving you multiple correct answers.
One more that's not on his list:
But if all you want to do is subtract, why bother changing the input to base 10?
The
dc
command is "desk calc". It will also take input from stdin, likebc
, but instead of using "order of operations", it uses stacking ("reverse Polish") notation. You give it inputs which it adds to a stack, then give it operators that pop items off the stack, and push back on the results.In the commands above we've got the following:
16i
-- tells dc to accept input in base 16 (hexadecimal). Doesn't change output base.BFCA3000
-- your initial number17FF
-- a random hex number I picked to subtract from your initial number-
-- take the two numbers we've pushed, and subtract the later one from the earlier one, then push the result back onto the stackp
-- print the last item on the stack. This doesn't change the stack, so...10o
-- tells dc to print its output in base "10", but remember that our input numbering scheme is currently hexadecimal, so "10" means "16".p
-- print the last item on the stack again ... this time in hex.You can construct fabulously complex math solutions with dc. It's a good thing to have in your toolbox for shell scripts.