The other day there was a thread on Channel9 where “orangie” was complaining about the fact that his programming class required him to learn how to convert between different bases (base 10->2->whatever).
In reality, it’s actually quite useful to know how to do the conversions, because knowing how the computer represents values can be remarkably important, especially in a CS course. In my case, we covered base conversions in the “intro to digital circuits” class, one of our projects was to write a circuit that would take a binary input (from a set of switches) and convert it to hex.
It turns out that base conversion is a core part of “New Math”, Tom Lehrer’s “New Math” song is all about how you do arithmetic in different bases. They’re still teaching “New Math” in classes, converting between bases is covered in the 5th or 6th grade at least at Wellington (the school where Valorie used to work). This makes sense, because it’s important that students have a fundamental understanding about how numeric operations work, and what makes up a number – understanding different base systems helps to figure it out.
Most of the time, I do my base conversions using this:
I’ve had it for over 20 years now, and it’s still going strong (gotta love HP’s engineering). Until I looked it up on eBay, I hadn’t realized it was worth about $100, go figure.
But, for those times that I need to convert by hand, it’s actually pretty simple, especially for some very common conversions (hex to binary, and hex to octal).
For hex to binary, all you need to do is to memorize the bit pattern for the 16 possible hex values (5 is 0101, 6 is 0110, f is 1111, etc).
Then it’s just a matter of string concatenation – you convert each digit to binary, string them together and convert back to whatever your destination base is.
Octal to binary is similar, you just have to remember values from 0-8 (which is easy if you’ve memorized the hex values :)).
My personal favorites are converting from hex to octal (and vice versa) because you don’t have to do any math. You do it by converting to a common base, binary in this case.
So 0x123456789abcdef in hex converts to:
0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
000 100 100 011 010 001 010 110 011 110 001 001 101 010 111 100 110 111 101 111
And converting from binary to octal:
Tada! Hex to octal conversion without having to do ugly math 🙂
Obviously this works for any power of two, it’s just that octal and hex are the two most common.
Now once you want to include converting to decimal, it’s gets a bit harder – then whipping out the calculator’s usually the best solution. If you still MUST do the conversion by hand, it’s often easier to do it with the octal representation than the hex representation. In this case, it’s:
Oh, and before someone asks, no, they don’t expect the 5th graders to convert numbers that are that large 🙂
Edit: Toned down the language about orangie, and added the comment about Tom Lehrer’s “New Math” song, it wasn’t my intention to embarass orangie.