Calculator via CLI

As an engineer dealing with storage I do get a hell-of-alot of info in either binary, hex, octal or decimal format. It’s always a PITA to convert these to human readable format. So i cooked up some bc functions and use these as a function in bash to beat the time wasted firing up the calculator or some weird stuff in LibreOffice Calc. Add the below in the .bashrc file and restart your shell.

function h2d { echo “obase=10; ibase=16; $( echo “$*” | sed -e ‘s/0x//g’ -e ‘s/\([a-z]\)/\u\1/g’ )” | bc; }
function h2b { echo “obase=2; ibase=16; $( echo “$*” | sed -e ‘s/0x//g’ -e ‘s/\([a-z]\)/\u\1/g’ )” | bc; }
function b2d { echo “obase=10; ibase=2; “$*”” | bc; }
function b2h { echo “0x$(echo “obase=16; ibase=2;”$*”” | bc)”; }
function d2b { echo “obase=2; ibase=10; “$*”” | bc; }
function d2h { echo “0x$(echo “obase=16; ibase=10; “$*”” | bc)”; }

So on the bash prompt:

[1423][erwin@monster:~]$ h2d 0x16

Very handy.

Cheers, Erwin

Print Friendly, PDF & Email

Subscribe to our newsletter to receive updates on products, services and general information around Linux, Storage and Cybersecurity.

The Cybersecurity option is an OPT-OUT selection due to the importance of the category. Modify your choice if needed.

Select list(s):