Pergunta

today i found the 'bc' linux command and found a strange behaviour when calculating with outputbase 10.

echo "ibase=16;obase=9;AFBE" | bc
67638

echo "ibase=16;obase=11;AFBE" | bc
09 02 11 08

echo "ibase=16;obase=10;AFBE" | bc
AFBE

well, command 1 and 2 are correct, but the third command simply prints the inputvalue.

echo "ibase=16;AFBE" | bc
44990

gives a correct result.

Is there any reason in this behaviour?

Foi útil?

Solução

You're not the first person to be caught by this. Apparently you need to set obase before ibase:

echo "obase=10;ibase=16;AFBE" | bc
44990

Outras dicas

Obviously, bc uses your ibase when it reads obase: that's why obase=10 always means "the same as ibase".

In the latest example, you don't give obase=10 (which would set the value to decimal 16), that's why obase remains the default (decimal 10).

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top