Calculator via CLI

Print Friendly, PDF & Email

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
22
[1423][erwin@monster:~]$

Very handy.

Cheers, Erwin

About Erwin van Londen

Master Technical Analyst at Hitachi Data Systems
Linux

Comments are closed.