[CentOS] [OT] GNU bc base conversion

Mon Aug 31 16:50:59 UTC 2015
Gordon Messmer <gordon.messmer at gmail.com>

On 08/31/2015 05:55 AM, Mike - st257 wrote:
>>> I'm not an expert in bc, so I might be wrong, but it looks like setting
>> >the ibase inside a function is simply too late.  ibase affects how bc
>> >interprets input.
>> >
> Thanks Gordon.
> Big bummer given that behavior. :-/

I suppose that depends on what you're trying to accomplish.  Most 
conversions you can do entirely within bash, if that's your goal.

function dec_to_hex () { printf '%x\n' $1; }
function hex_to_dec() { printf '%d\n' $(( 16#$1 )); }
function hex_to_bin() { echo 'obase=2;' $(( 16#$1 )) | bc; }

$ dec_to_hex 10
a
$ hex_to_dec a
10
$ hex_to_bin a
1010