Converting between bases...

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

or:

000100100011010001010110011110001001101010111100110111101111

Regrouping:

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:

04432126361152746757

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:

    4*180124248109481984
+  4*22515531013685248
+  3*2814441376710656
+  2*351805172088832
+  1*43975646511104
+  2*5496955813888
+  6*68719476736
+  3*85889934592
+  6*1073741824
+  1*134217728
+  1*16777216
+  5*2097152
+  2*262144
+  7*32768
+  4*4096
+  6*512
+  7*64
+  5*8
+  7
-------------------------------------
81985529216486895

Easy, right?

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.