Saturday, February 28, 2009

Bash - binary hexadecimal conversion using wcalc

wcalc - a natural-expression command-line calculator

Just came through wcalc, which is a command-line calculator designed to accept all valid mathematical expressions.

From man pages of wcalc:
It supports all standard mathematical operations, parenthesis, brackets, trigonometric functions, hyperbolic trig functions, logs, and boolean operators.

If no mathematical expression is given at the command line, wcalc enters "interactive" mode. Interactive mode has more features. Also, files may be piped to wcalc, and they will be interpreted.

Install wcalc in your Ubuntu or Debian:
$ sudo apt-get install wcalc

The first thing I tried with wcalc is hexadecimal, binary, octal, decimal conversion. It gives ready made option for the same, which I am going to explain in this post. Later I will put a detail post on its other usages.

In wcalc output:

- Number starting with 0x are interpreted as hexadecimal
- Number starting with 0b are interpreted as binary
- Number starting with 0 are interpreted as octal

Some of the sample conversions:

#Hexadecimal(base 16) of 13
$ wcalc -h 13
= 0xd

#Binary(base 2) of 5
$ wcalc -b 5
= 0b101

#Octal(base 8) of 11
$ wcalc -o 11
= 013

#Decimal(base 10) of binary 111
$ wcalc -d 0b111
= 7

#Option -q(quiet), Toggles whether the equals sign will be printed before the results.
$ wcalc -q -b 5

Related post:
- Bash desk calculator dc explained
- Hex to decimal conversion in bash shell

No comments:

© Jadu Saikia