[CentOS] [OT] GNU bc base conversion

Mike - st257

silvertip257 at gmail.com
Fri Aug 28 14:15:10 UTC 2015


Hello CentOS List Members,

Thoughts as to why my BC functions aren't properly converting between bases?

Decimal to binary or hex works fine, but not binary or hex to decimal and
so forth.
No doubt the syntax is in some way wrong, but when I test from the CLI and
the right values are returned, I have to wonder.

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" | bc
171
]$ echo "obase=A; ibase=16; AB" | bc
171

]$ grep hex_to_dec ~/.bcrc
define hex_to_dec(h) { obase=A; ibase=16; return h; }

-- 
---~~.~~---
Mike
//  SilverTip257  //



More information about the CentOS mailing list