Linux Commandline Calculator - zcalc

Posted on Mon 13 January 2020 in Computing

I quite often need to be able to convert hex to binary for my MegaDrive assembly writing and a command line calculator would be useful for doing this - I've never really got on with BC.

I came across zcalc which is part of zsh. It can be enabled with

autoload zcalc

In your .zshrc

Although it has an interactive shell, I'm more interested in using it for one-off commandline commands. Here's some examples

> zcalc -e "1+1"
2

# Add some hex values but return decimal
>zcalc -e '0xA200 + 0x0100'
41728

# Convert binary to hex
>zcalc -e '-#16' 0b11111111
16#FF

# Convert hex to binary
> zcalc -e '-#2' 0xA200
2#1010001000000000

# Mix and match
> zcalc -e '-#16' '0xA200 + 256 + 0b1111'
16#A30F

See man zshcontrib for more details