[Chapter 49] 49.3 Gotchas in Base Conversion
Author: Jerry Peek, Tim O'Reilly & Mike Loukides
Date: 1998-08-04T21:56:40Z

#

| Chapter 49 Working with Numbers |

## 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

49.2 bc: Hexadecimal or Binary Conversion |  | 49.4 bc's Sine and Cosine Are in Radians