[problem]
You want to perform some arithmetic with UNIX.
[/problem]
[solution]
The binary calculator, installed with pretty much all version of UNIX, can be used for an array of tasks.
Not just basic calculations, but also converting between bases and it can automated, etc.
[/solution]
[example]
It’s simpliest use, it just to run bc and add, subtract divide.
bc
888+999
1887
Be aware though, that to perform precision point calculations, you must specify the “-l” long option. For example:
bc
999/222
4
Obviously wrong! 🙂 Now with the long option:
bc -l
999/222
4.50000000000000000000
Can also be automated like this, to convert between decimal and hex:
i=0; while ((i < 20)) ; do echo -n ' $i: '; echo 'base=10;obase=16;$i' | bc; ((i++)); done | paste - - - -
0: 0 1: 1 2: 2 3: 3
4: 4 5: 5 6: 6 7: 7
8: 8 9: 9 10: A 11: B
12: C 13: D 14: E 15: F
16: 10 17: 11 18: 12 19: 13
[/example]
[reference]
[tags]BC, HEX, Unix Coding School[/tags]
[/reference]
If you have found my website useful, please consider buying me a coffee below 😉