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.

1. Anonymous says:

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

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

2. Anonymous says:

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.

3. Alex, that one gets the wrong answer:

0x123456789abcdef = 8.19855292 × 10^16

It’s a close approximation, but not accurate.

4. Jeff Parker says:

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.

5. Anonymous says:

My solution is even nerdier than Larry’s!

http://spaces.msn.com/members/jvert/Blog/cns!1pZgrp-a2ufyKDqoWoA6v47g!211.entry

6. Anonymous says:

Come on Larry, you totally left out fractions. Let’s see some IEEE 754 Floating point numbers, and converting between bases with them. ðŸ˜‰

7. Manip says:

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… ðŸ™‚

8. Anonymous says:

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!

9. Anonymous says:

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.

10. 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 ðŸ™‚

11. Anonymous says:

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

12. Anonymous says:

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.

13. Anonymous says:

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.)

14. Gael, yup, it’s RPN.

15. Anonymous says:

it’s not about bases!! I know my ones,tens,hundreds etc, It was converting hex to binary..did you learn that in 5th grade?

16. Anonymous says:

A computer with an AMD64, 512MB of RAM, 80GB hard disk, and 1280×1024 LCD screen costs the same as my HP-35 did. But it’s less powerful ^u^

Proof:

CALC.EXE, even in scientific mode, won’t do RPN.

17. Orangie, I didn’t. But I was in 5th grade in 1972.

My son did, otoh.

18. Anonymous says:

It is a bit off the subject, but hey I’ll give it a go.

It is interesting to me that our fellow brothers in US say/write math while us Antipodeans (Aussies/Kiwis) say Maths.

Regards,

Richard

19. Anonymous says:

Orangie, it *is* about bases. Converting hex to binary is to convert from base 16 to base 2.

Anyway, I’ve never really understood when to use base 8. I do most of my "computerish" calculations in base 16 (hex) or base 2 (binary).

20. JamesW says:

dc

skaro:~ james\$ echo 16i0x123456789ABCDEFp | dc

81985529216486895

Although if I’m doing a lot of conversions I’ll use it interactively.

21. Anonymous says:

Haven’t seen one of those old HPs in many a year. I have one somewhere but not sure if it still works. Guess I’ll have to go hunting through some old boxes…

22. Anonymous says:

Just as a quick plug, I’ve written a web page that explains how to do conversions between bases: http://www.golgotha.org.uk/useful/bases.html

I’m not sure how the US grade system works (i.e. how old 5th graders are), but I learnt this stuff when I was about 11.

24. Anonymous says:

This is more to do with hex arithmetic than changing bases, but I was recently given an old-style chinese abacus, where beads are in columns, with 2 beads at the top (each worth 5), then a divider and 5 more beads below (each worth 1).

Learning to use them is a nice intellectual exercise, but I was intrigued by the fact that you only use 1 upper and 4 lower beads to do calculations.

It turns out you use them all in hexadecimal arithmetic, because the old chinese system of weights and measures was in hex.

I’ve tried it out, and so long as you remember to count using A to F instead of 10 to 15, it’s surprisingly usable for addition and subtraction, and older than your calculator by about 700 years!

25. Anonymous says:

Manip,

I must respectfully disagree with your assessment that conversions are useless outside of school. I agree that for big numbers, calc is probably better, but I have found it useful a number of times.

For example, I often deal with code for embedded systems where bit fields can be written either 0xFF or 0b11111111.

More generally, take the case of flag masks. If I want to know which bits are set in 0xE3, this is a hex–>binary conversion.

In both cases, this is much more quickly done in my head rather than breaking concentration to start up calc.

I’ve never sat down and memorized the 16 conversions–I just found occasion to use them enough that it sticks.

Octal, on the other hand, I have never found occasion to use outside of school… ðŸ˜‰

26. Alas, humans are most comfortable with base 10.

All the computer bases… binary (2), octal (8), hexadecimal (16), and base64 (64)… can be translated one-to-the-other a piece at a time (as long as you’ve got the offsets right) via the regrouping and translation tables you demonstrate above. Easy.

But converting from any of these to decimal — and from decimal to any of these — requires the BigInt-style multiplication and accumulation. Yuck.

So orangie’s original problem – convert from binary, to decimal, to hex – is an exercise in torture, no? Leaving decimal out would have been much easier.

27. Anonymous says:

Yes, the kids in the 5th/6th grade class where I worked for the last two years learn to add, subtract, multiply, divide and convert between base 10, base 2, base 3, base 5, base 7, base 8, base 9, base 11, base 12 and base 16. Kids are between 10 and 12 years of age for the most part. It’s a big part of the place value unit, but also a sneaky way to have kids practice their base10 basic math skills. Kids usually write base10 to base X conversion tables from 1 to 100 for each base as the intro, but we’ll practice converting numbers up to 1M. After all, if you can convert 50, then converting 500,000 just takes more places.

Another point to make is that European and Arabic math is base10 but many other cultures did not use base10 historically. Babylonions used base60 because it had so many factors. There’s a modern day African tribe that uses a numbering system akin to base28 because they recognize 28 different body places when counting.

Is converting bases useful to daily life? Not really. Calculators are way too prevalent in life, but I think understanding how bases work is essential being a well educated person in today’s day and age. I think it’s one of those foundation blocks in math that makes one’s understanding of higher concepts pretty shaky if it isn’t solid, but then again, I’m a definite math geek…

28. > Octal, on the other hand, I have never found occasion to use outside of school…

I have. It’s used for UNIX file permission masks, for example.

29. Anonymous says:

Well, all your base(s) are belong to us.

30. Anonymous says:

Great post Larry.

Re: The octal/base-8 chatter. The old DEC stuff used to use octal a ton, you had to know octal almost as well as you knew decimal for some stuff as you saw it more often. It was actually weird going from a DEC PDP to a PC and dealing with a Hex instead of Octal.

I find that many of the younger coders and admins now have little understanding of the standard computer bases nor even bitwise logic and how critically important it all is. They may be learning it in 5th grade but they aren’t remembering it very well when they get to the real world. Lots of core basics like that don’t seem to be understood as well as it should be, it is why I always recommend Petzold’s book "Code" to anyone I meet in the industry as required reading. We may have all of these "advanced" languages and frameworks like Dot NET / Java / etc but it all still comes back to 0’s and 1’s.

joe