start page | rating of books | rating of authors | reviews | copyrights

UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 49.2 bc: Hexadecimal or Binary Conversion Chapter 49
Working with Numbers
Next: 49.4 bc's Sine and Cosine Are in Radians
 

49.3 Gotchas in Base Conversion

Say you want to convert between octal and hex, and you type:

% bc
ibase=8
obase=16
17
11

It looks like something's gone haywire. 17 octal ought to be F hexadecimal, not 11. What's wrong?

In fact, since you set ibase to octal before you set obase, what you really asked for was obase equal to 16 octal (14 decimal)!

In short, always set obase before ibase, or you're in for a surprise.

Similarly, suppose you'd set ibase to 16, and want to set it back to 10. You need to type:

ibase=A

not:

ibase=10

Another thing to look out for is typing a hex digit when ibase is set to something other than 16. bc isn't smart enough to reject the input, and gives you back garbage.

- TOR


Previous: 49.2 bc: Hexadecimal or Binary Conversion UNIX Power ToolsNext: 49.4 bc's Sine and Cosine Are in Radians
49.2 bc: Hexadecimal or Binary Conversion Book Index49.4 bc's Sine and Cosine Are in Radians

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System