Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert hexadecimal number to decimal whith shell script

Tags:

shell

sh

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?

like image 308
developer Avatar asked Dec 01 '22 22:12

developer


1 Answers

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
like image 155
Andriy Tykhonov Avatar answered Dec 04 '22 06:12

Andriy Tykhonov