Displaying 2 results from an estimated 2 matches for "bin_to_dec".
2015 Aug 28
2
[OT] GNU bc base conversion
...Other than a few other define statements, the only other option I have set
is scale=5 in my bcrc.
In reference to the order of (o|i)base parameters, I have specified obase
before ibase [0].
[0] http://docstore.mik.ua/orelly/unix/upt/ch49_03.htm
See below for my examples.
Thanks!
]$ echo "bin_to_dec(1001)" | bc
1001
# should be decimal 9
]$ echo "obase=10; ibase=2; 1001" | bc
9
]$ grep bin_to_dec ~/.bcrc
define bin_to_dec(b) { obase=10; ibase=2; return b; }
----
]$ echo "hex_to_dec(AB)" | bc
99
# should be decimal 171
]$ echo "obase=10; ibase=16; AB" | b...
2015 Aug 28
0
[OT] GNU bc base conversion
...tween bases?
>
> Decimal to binary or hex works fine, but not binary or hex to decimal and
> so forth.
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. So "echo "bin_to_dec(1001)" | bc" is going to
interpret the value of 1001 while reading it from input, not after
passing it to a function where ibase is reset.
Supporting that theory:
$ bc
...
define bin_to_dec(b) { obase=10; ibase=2; return b; }
bin_to_dec(1001)
1001
Decimal to binary and hex work correc...