Setting ibase
means you need to set obase
in that same base. Explaining your examples will show this:
echo "ibase=F;obase=A;C0" | bc
You set bc
to consider input numbers as represented in base 15 with the "ibase=F". "obase=A" sets output numbers to base 10, which is the default.
bc
reads C0 as a base 15 number: C = 12. 12*15 = 180.
echo "ibase=F;obase=10;C0" | bc
In this one, you set input to base 15, and output to 10 - in base 15, so output base is 15. C0 input in base 15 is C0 output in base 15.
echo "ibase=16;obase=A;C0" | bc
Set input to base 16, output to base 10 (A in base 16 is 10 in base 10).
C0 converted to base 10 is: 12*16 = 192
My personal rule is to set obase first, so that I can use base 10. Then set ibase, also using base 10.
Note that bc
does have an ironic exception: ibase=A
and obase=A
always sets input and output to base 10. From the bc
man page:
Single digit numbers always have the value of the digit
regardless of the value of ibase.
This behavior is enshrined in the specification of bc
: From the 2004 OpenGroup bc
specification:
When either ibase or obase is assigned a single digit value from
the list in 'Lexical Conventions in bc', the value shall be assumed
in hexadecimal. (For example, ibase=A sets to base ten, regardless
of the current ibase value.) Otherwise, the behavior is undefined
when digits greater than or equal to the value of ibase appear in
the input.
That's why the ibase=F
setting changed your input base to base 15, and why I recommended to always set the base using base 10. Avoid confusing yourself.