Larry Osterman's WebLog

Confessions of an Old Fogey
Blog - Title

Converting between bases...

Converting between bases...

  • Comments 35
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.

 

  • According to this site, it was approx. $150US brand new.

    http://www.rskey.org/detail.asp?manufacturer=Hewlett-Packard&model=HP-16C
  • Larry,

    There is of course the FREE* Calc.exe, which in scientific mode, offers DEC / HEX / OCTAL / BINARY conversions.

    Cheers,

    Dale.

    * - well, ok, you need to license the OS.
  • http://www.google.com/search?hl=en&lr=&q=0x45f3+in+decimal
  • Alex, that one gets the wrong answer:
    http://www.google.com/search?hl=en&lr=&safe=off&c2coff=1&q=0x123456789abcdef+in+decimal

    0x123456789abcdef = 8.19855292 × 10^16

    It's a close approximation, but not accurate.

  • Heh, you know it is funny you mention good old HP products, I still have a lot of them laying around or boxed up around. Old HP Products are kind of like old Muscle Cars, they run strong, easy to work on and built to last a lifetime. I have an old HP Jornada running the first windows CE, black and white display. your lucky if the batteries lasted 2 hours while on and running, but was cool for it's time.
  • My solution is even nerdier than Larry's!

    http://spaces.msn.com/members/jvert/Blog/cns!1pZgrp-a2ufyKDqoWoA6v47g!211.entry
  • Come on Larry, you totally left out fractions. Let's see some IEEE 754 Floating point numbers, and converting between bases with them. ;)
  • After school/college converting by 'hand' is useless ... I mean it is handy to know how each number system relates to the other and why they exist but memorising all sixteen hex to binary conversion? No thanks. I got though my Universities 'basic' maths exam which funnily enough I found harder than the actual maths in my unit (the basic maths is all the conversions that you never have to use in the actual course).

    Now we are doing things like Sets, complex equations that don't require you to memorize stuff, you just have to learn the "logic" and you can work out the solutions, not remember the solutions and work out the logic or something like that... :-)
  • hello Larry, Thanks for making me look stupid.

    It wasn't converting between bases, we couldn't use a calculator and It was for Computer Programming 1, I got it now (used Wikipedia.org).

    It was converting Hex to Binary, What school do you teach 5th grade students Binary?..

    I understand it..

    the Quiz Question was convert 1331^10 to Binary.. 1331 = leet ;)

    which is 10100110011^2

    I might miss a 0 or 1 somewhere but yes yes..

    Btw thanks for making me look stupid!

  • My son is now in Grade 5 here in Vancouver BC and they are "reviewing" the place value system - base 10. Teaching grade 5 different base systems might be a bit too early for most kids methinks. When I was in school in Hong Kong I learn the conversion by hand in grade 7 - first year highschool. But then again we learn other useless things like converting between roman numerals and decimals. Also looking up the old log tables for logs, square roots, sine cosines and the like.
  • orangie, it wasn't my intent to make you look stupid, I'm sorry if that was the effect.

    Actually I was just using this as a lead-in for the "how do I convert from hex to octal" trick.

    And John, you're right, you out-geeked me :)

    I had no idea about the .formats command.

  • well it's okay, the reason I didn't get it because the prof. was in a hurry because all of the CS,CIS,Engineering students couldn't get in MSDN AA.. (free software :))

    btw sent you a email
  • Hey larry,
    Is this one of those calculators that use the reverse polish notation for input? My Electronic Devices' professor has one just like yours. I wouldn't be surprised if it was.
  • Orangie,

    I live in Hong Kong, and we were taught about multi-base(2, 8, 10, 16) conversion at Primary 6(somehow equivalent to Grade 6 there). It's on the curriculum for Maths. published by our Education Dept., but of course convertion for such big number is not included. (Maybe the students who attend Maths. Olympics will have to learn it anyway.)
  • Gael, yup, it's RPN.
Page 1 of 3 (35 items) 123