Caffeinated Bitstream

Bits, bytes, and words.

Lowercase hexadecimal hack for GNU bc

I frequently use the command-line GNU bc calculator to do quick arithmetic while debugging C programs. An ongoing annoyance is bc's insistence that the hexadecimal digits A-F must be entered in uppercase. Other software, such as GDB and printf("%p",...), output lowercase hexadecimal digits, which keeps me from cutting and pasting the values into bc. Manually typing the values can be a big time sink when I need to perform a lot of calculations.

I finally got fed up with this behavior and made a version of bc with the following modifications:

  1. Hexadecimal digits may now be entered in lowercase.
  2. Input and output defaults to base 16.
I named this alternate version "xc" and use it when I'm performing pointer arithmetic while debugging. Note that since the lowercase letters a-f are now regarded as hexadecimal digits, some bc functionality such as functions and variables may be impacted as a side effect.

Here are links to the baseline bc source code, and the patch with my modifications:

Here's a cheatsheet for compilation. I needed to install the libreadline5-dev package on my Ubuntu system to compile with readline support, which is needed if you want to use cursor keys to edit your input.
tar xvfpz bc-1.06.tar.gz
patch -p0 < bc-lowercase-hex-hack.patch
cd bc-1.06
./configure --with-readline
make
sudo mv bc/bc /usr/local/bin/xc

Update, April 22nd, 2012: The exact version of bc that works with this patch disappeared from prep.ai.mit.edu, so I'm changing the link to a locally hosted package. (This version is still available at ftp.gnu.org, for now.)