Site Navigation

Jul 17, 2013

bc - Basic Calculator

Linux Logo

I stumbled across this today, a way to do hex conversions from the command line. Not only did I find such a tool, but I found a pretty cool calculator as well. bc is a GNU command line tool included with most Unix distros (OS X, Linux).

Hex to Decimal Conversion

With bc you can do basic conversions from the command line in a manner like this:

echo "ibase=16; hex-number"|bc
echo "ibase=16; F"|bc

Sample output:

15

By default, bc performs all operations in decimal. To convert from hexidecimal to decimal set the input base system to hexidecimal with the command “ibase=16;”. Then, any hex number will be converted to decimal for you.

Decimal to Hex

To convert to decimal to hexidecimal, set the output base to sixteen with “obase=16;”. For example:

echo "obase=16; decimal-number"|bc
echo "obase=16; 10"|bc

Sample output:

A

Decimal to Binary

And of course you can go decimal to binary:

echo "obase=2; 7"|bc

Sample output:

111

It does Math too

You are not limited to conversions you can do math too:

echo "2+2"|bc

Sample output:

4

bc Interactive Mode

Finally, I must mention you do not have to do everything from the command line. If you just run bc from the command line, you can do all of this interactively.

Post a Comment

Favorite Links Feed