How to convert Decimal to Octal, binary, Hexadecimal from command line
when you work at routing, you will find yourself converting decimal to binary and the opposite, specially for the masks of the IP address, here I will show you how to do it easily using your command line terminal.
Decimal to Hexadecimal
echo 'obase=16;10'| bc
A
Or
wcalc -h 10
Decimal to Octal
echo 'obase=8;10' | bc
12
Or
wcalc -o 10
Decimal to Binary
echo 'obase=2;10' | bc
1010
Or
wcalc -b 10
From Hexadecimal to decimal
echo 'ibase=16;A' | bc
10
From Octal to Decimal
echo 'ibase=8;12 | bc
10
From Binary to Decimal
echo 'ibase=2;1010 | bc
10
Note: Be sure to have bc or wcalc installed on your system
If you have a value in hex format and want to see its decimal value, you can easily convert it using the terminal. To convert 3f hexadecimal type (in a bash shell):
$ let x=0x3f
$ echo $x
63
$ let x=0xfffe
$ echo $x
65534
isntead of two commands, you can do it using arithmetic expression interpolation:
echo $[0xfffe]
printf is a bit more flexible, you can convert from hex back into decimal,
etc. It works just about like the C library function, only you run it from
the shell.
Here's how you could do it as a Perl one-liner:
perl -e 'print 0x3f, "\n"'
or
perl -e 'printf("%d\n", 0x3f)'
Two bash aliases that might help out...
alias h2d='printf "%d\n" ${1}'
alias d2h='printf "0x%x\n" ${1}'
Hex, Octal and Binary shell conversions
Hex, Octal and Binary shell conversions
In scripting, it is common to find a need hexadecimal (hex or base 16) numbers, and occasionally binary numbers. Converting can be tricky without some tips. You can use printf, typeset and even bc as conversion tools. Here are some ideas: