Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Try bc(1) too' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Try bc(1) too
Authored by: mkhaw on Mar 04, '03 11:46:48AM

bc is also able to do radix conversions in just about any radix (expressible in numerals and uppercase letters). For example, for hex to decimal...

tcsh% bc
ibase=16
3F
63
[ctrl-D]
tcsh% 

Or, for a shell 1-liner...

tcsh% echo "ibase=16; 3F" | bc
63
tcsh% 
Defining a csh alias or sh function named hex2dec is left as an exercise for the reader :^)!

Note (1) you have to use uppercase, (2) once you specify a non-decimal input base (via ibase=...) ALL your input is in the new radix until you change it.

bc is pretty powerful, capable of arbitrary precision math, function definition, complex C-like flow control expressions, etc. See the man page for details.



[ Reply to This | # ]
Don't forget obase
Authored by: wfolta on Mar 04, '03 12:34:14PM
You can convert from, say, octal to hex by:

obase=16
ibase=8

Note that obase is first since ibase changes your input base for all following commands, including the obase command if you reversed the order.

[ Reply to This | # ]