1

Right now I'm preparing for my AP Computer Science exam, and I need some help understanding how to convert between decimal, hexadecimal, and binary values by hand. The book that I'm using (Barron's) includes an example but does not explain it very well.

What are the formulas that one should use for conversion between these number types?

3
  • If this is a final exam, I hope you fail the test. Maybe management is better suited for you? Feb 19, 2012 at 0:57
  • 2
    I think you're misunderstanding something here; the test that I'm taking is the AP Computer Science A exam put out by Collegeboard. I'm currently in Computer Science 1 as a junior, and my teacher has only went over the basics so far (primitives, boolean algebra, encapsulation, scope, arrays, arraylists, etc.). He has not gone over binary/hexadecimal values at all. I had to teach myself recursion, GridWorld, insertion and selection search, inheritance, and other concepts.
    – andyf
    Feb 19, 2012 at 6:34
  • I think you falsely assume the everybody know what "AP Computer Science exam" is. Also there is no such thing as hexadecimal value. The value stays the same, it is only the (re)presentation that is chosen for the number. 10 and 0x0a and 012 have the same value but are represented differently. Feb 19, 2012 at 12:12

2 Answers 2

2

Are you happy that you understand number bases? If not, then you will need to read up on this or you'll just be blindly following some rules.

Plenty of books would spend a whole chapter or more on this...

Binary is base 2, Decimal is base 10, Hexadecimal is base 16.

So Binary uses digits 0 and 1, Decimal uses 0-9, Hexadecimal uses 0-9 and then we run out so we use A-F as well.

So the position of a decimal digit indicates units, tens, hundreds, thousands... these are the "powers of 10"

The position of a binary digit indicates units, 2s, 4s, 8s, 16s, 32s...the powers of 2

The position of hex digits indicates units, 16s, 256s...the powers of 16

For binary to decimal, add up each 1 multiplied by its 'power', so working from right to left:

1001 binary = 1*1 + 0*2 + 0*4 + 1*8 = 9 decimal

For binary to hex, you can either work it out the total number in decimal and then convert to hex, or you can convert each 4-bit sequence into a single hex digit:

1101 binary = 13 decimal = D hex

1111 0001 binary = F1 hex

For hex to binary, reverse the previous example - it's not too bad to do in your head because you just need to work out which of 8,4,2,1 you need to add up to get the desired value.

For decimal to binary, it's more of a long division problem - find the biggest power of 2 smaller than your input, set the corresponding binary bit to 1, and subtract that power of 2 from the original decimal number. Repeat until you have zero left.

E.g. for 87:

  • the highest power of two there is 1,2,4,8,16,32,64!
  • 64 is 2^6 so we set the relevant bit to 1 in our result: 1000000
  • 87 - 64 = 23
  • the next highest power of 2 smaller than 23 is 16, so set the bit: 1010000
  • repeat for 4,2,1
  • final result 1010111 binary
  • i.e. 64+16+4+2+1 = 87 in decimal

For hex to decimal, it's like binary to decimal, only you multiply by 1,16,256... instead of 1,2,4,8...

For decimal to hex, it's like decimal to binary, only you are looking for powers of 16, not 2. This is the hardest one to do manually.

0

This is a very fundamental question, whose detailed answer, on an entry level could very well be a couple of pages. Try to google it :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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