Tell me more ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I know binary, but have never really tried to learn Octal or Hex. Does anyone have any good references for them? Or, any suggestions for a book or something that talks about different bases of numbers?

share|improve this question
@Josh, just slog through it long enough and you'll get the hang of it. No need to sweat the formal stuff. It is not like you are going to do major maths with it, is it? Usually that sort of stuff is for debug purposes, so you need to recognise patterns, maybe add, but then you should have a calculator to be sure at the start and additions come in easy enough. You probably will not need to multiply of the top of your head, so no need to learn that. – asoundmove Feb 26 '11 at 2:28

7 Answers

up vote 20 down vote accepted

If you already know binary, you're 99% of the way there. I'd recommend just reading Wikipedia (here and here). Here's why:

Octal and hex are best thought of as shorthand for binary. If you can express a number in binary, it is trivial to convert it to hex or octal, and vice-versa, when you consider that the radices of these bases (8 and 16) are powers of 2 (the radix of binary). Here is how to do the conversion:

Consider the binary number 101001011100. Since 2^3 is 8, split it into groups of 3 starting from the least significant bit and padding zeros as you go:

101 001 011 100

Now, convert each group to its "decimal" representation:

5 1 3 4

So 101001011100 in base 2 is 5134 in base 8. Same for hexadecimal, but split it into groups of 4:

1010 0101 1100

And again convert each group into the corresponding value, remembering that 10 = A, 11 = B, ... 15 = F.

A 5 C

So 101001011100 in base 2 is A5C in base 16.

share|improve this answer
1  
+1 for the shorthand for binary. I've met people who use hex all the time without knowing this, and marvel why they would bother when they're completely missing the point. – Karl Bielefeldt Feb 25 '11 at 23:54
2  
Note that the most common words are not multiples of three bits (they tend to be power of two multiples of 8 bits), so you might need to make sure to say groups of three starting from least significant bit (usually written rightmost). – asoundmove Feb 26 '11 at 2:26
1  
@asoundmove - C was invented on a machine with 9bit bytes so 3octal digits made more sense that 2.1 hex digits – Martin Beckett Feb 26 '11 at 5:06
+1 - really nice clear explaination. – Qwerky Aug 5 '11 at 10:26

You need to learn the concept of bases (or radix) in general.

Think of numbers as a load of digits, the column a digit is in corresponds to its weight, every column you go up is a multiple of the base in which you are in. With decimal, base 10, each column can go from 0 to 9, when we reach ten, we have 1 ten and 0 units, so we write it as 10.

In octal it is base 8, the digits go from 0 to 7, when we get up to 8, we write 10 again, 1 eight, and 0 units. Hexadecimal (hex = 6, dec = 10 --- base 16) is the same, digits go from 0 to F (that's 0-9 and a-f to represent 10 to 15 as single digits). When we get up to 16, we write 10, 1 sixteen and 0 units. Fifteen is F, the last digit, 31, (16 + 15) is 1F.

This link explains it quite nicely, starting off with the basic concept, running through binary well which is nice and simple and finishing with octal and hexadecimal: http://doit.ort.org/course/inforep/130.htm

We can use any base, 2-36 are easy to represent, as we have 26 characters and 10 numerals to represent the digits of the number. Always the same principle.

share|improve this answer

Wikipedia links: octal and hex should be a good starting point.

share|improve this answer

I use google calculator from time to time and this has improved my hex/octal knowledge

http://www.google.com/search?q=0xFF+to+decimal

http://www.google.com/search?q=0o10+to+decimal

The key is to have something at hand to translate.

share|improve this answer

I'll contribute the following (see chapter 3):
http://homepage.mac.com/randyhyde/webster.cs.ucr.edu/www.artofasm.com/Windows/HTML/AoATOC.html

This may not be all you need, but it may be helpful. In addition it provides information on related programming topics such as: bitwise operations, signed/unsigned number representation, shift/rotates, etc...

Just one warning, this is an assembly language book, so there are a couple of program examples you can safely skip. Everything you want is in volume 1 chapter 3.

share|improve this answer

In base 10, from right to left, you have a digit for ones, a digit for tens, a digit for hundreds, a digit for thousands and so on. Each digit leftward has ten times the weight.

In base 8, from right to left, you have a digit for ones, a digit for eights, a digit for sixty-fours, a digit for five-hundred-and-twelves and so on. Each digit leftward has eight times the weight. 8 decimal is 10 in octal, 64 decimal is 100 octal, 512 decimal is 1000 octal.

In base 16, from right to left, you have a digit for ones, a digit for sixteens, a digit for two-hundred-and-fifty-sixes and so on. Each digit leftward has sixteen times the weight. 16 decimal is 10 in hex, 256 decimal is 100 hex, 4096 decimal is 1000 hex.

In base n, each digit leftward has n times the weight. n is 10 in base n. n-squared is 100 in base n. n-cubed is 1000 in base n. If you take p repeats of n and multiply them together (ie n raised to the power p), the result is 1 followed by p zeros in base n.

Normally, a base is a whole number, two or greater. However, I've seen some math (fractal related) that works complex numbers as bases. In this case, "digits" can IIRC be any whole number, with no maximum.

share|improve this answer

Simply practice, practice, practice. I "know" octal, but never use it, so it takes me some time to do conversions. On the other hand, I use hex frequently and so I've become fluent in it. I can even do quite a bit of hex -> ASCII conversion in my head now. There is no "resource" I used to learn this, it's simply repetition of a task. Any decent calculator app on your computer will be able to do dec/hex/oct/bin conversions while you practice, and I prefer this site or this site (before my workplace blocked it) to get the feel for hex/ascii.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.