Base conversions with bc


Whiile reading through Planet Sun I stumbled across Chris Gerhard’s base conversion post. I replied with an alternate solution using bc(1), which I have used extensively through the years. bc(1) is a command line calculator that can convert between bases when the “ibase” and “obase” options are used. The following example shows how to convert a base 10 number to it’s hexidecimal equivalent:

$ bc
obase=16 12345677 BC614D

We can also use bc to convert a hexidecimal value to decimal:

$ bc
ibase=16 FF 255

This gets even better! We can convert a hexidecimal value to binary:

$ bc
ibase=16 obase=2 FF 11111111

I really really like bc (and dc)!

This article was posted by Matty on 2005-05-04 22:09:00 -0400 -0400