nick34992
2/19/2017 - 7:47 PM

Bash: echo: obase, ibase, bc: convert binary to decimal, convert decimal to binary

Bash: echo: obase, ibase, bc: convert binary to decimal, convert decimal to binary

#!/bin/bash

dec=100     # decimal
bin=1100100 # binary

echo "obase=2;$dec" | bc      # 1100100 (decimal to binary)
echo "obase=10;ibase=2;$bin" | bc # 100 (binary to decimal)