Base conversion in Ubuntu (decimal to binary)

Posted on Thu 11 January 2018 in tips, linux

Need to convert a base 10 integer in a base 2 one? Or, at any rate, convert a number from one numeration system to another? In Ubuntu, the bc utility already integrates these features. It is usually already installed, so you don't have to anything special.

Simply run bc, and enter the following commands:

ibase=10
obase=2

Then, all subsequent number inputs will be simply converted to their base-2 representation.

terminal

If you want to get a conversion straight ahead, without going through the opening of bc, just enter the following from a terminal:

echo 'obase=2; ibase=10; 123' | bc

which will convert the number 123 from base 10 to base 2.

terminal

Of course, 2 and 10 can be replaced with any other possible base!