Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
I need to convert a number from hexadecimal form to decimal form using shell script
for example :
convert() { ... echo x=$decimal }
result :
convert "0x148FA1" x=1347489
How to do it?
You can convert in many different ways, all within bash, and relatively easy.
To convert a number from hexadecimal to decimal:
$ echo $((0x15a)) 346 $ printf '%d\n' 0x15a 346 $ perl -e 'printf ("%d\n", 0x15a)' 346 $ echo 'ibase=16;obase=A;15A' | bc 346
Add a comment
You can check this link. Fundamentally below code will help you.
h2d(){ echo "ibase=16; $@"|bc } d2h(){ echo "obase=16; $@"|bc }
It is in pure shell scripting solution.
If you have a python interpreter available, you can always do:
#!/bin/sh x="0x23" val=`python -c "print int('$x', 16)"` echo $val
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.