IDG Contributor Network: EZ conversions on Unix
Converting numbers from decimal to binary to hex to decimal to hex to binary back to decimal might cause some people to, if not pull out their hair, pull up a calculator on their desktops, but Unix users have some other options. And these options can be very helpful in scripts where you might need your numbers in some particular format.
One such option is bc -- the arbitrary precision calculator. Just open a terminal (or use the one you undoubtedly already have open) and try some easy calculations to see how this works.
$ echo "obase=10;ibase=2;101" | bc 5
So, what did we just do? Using ibase, we set the base of the number provided (in this case, 101) to 2. This means that we’re telling bc that the number we want converted is currently in binary. We used obaseto set the base that we want the output to be provided in to 10, so we want to see the number 101 converted to decimal. And, as you can see, it did a good job. Binary 101 is decimal 5.
To read this article in full or to leave a comment, please click here