View Single Post
Old 09-03-2010, 12:16 AM   #1
scarecrow
Junior Member
 
scarecrow's Avatar
 
Join Date: Aug 2010
Location: Texas
Posts: 13
Post Convert Between Decimal, Hex, and Binary (And a Hex Explanation) For Dummies

I hope I put this in the right place. If not, I would like it if you would pm me as to where you moved it.

I wrote this in a FaceBook comment for a friend. I figured it might be a good addition here.

Quick overview of Hex:
Hexadecimal is a numbering system in it's own. It's very different from our own, but very much the same as well.
Our system is base 10, which means that when we count by our current numbers (10 of them):
0 1 2 3 4 5 6 7 8 9
We add 1 to the next place, and then set the previous place to 0.
So when going up from 9, you set the one's place to 0, and add 1 to the ten's place, getting 10. Or if you are going up from 199, then you set the ten's and the one's place to 0, and add 1 to the hundred's place. This gives you: 200

Hexadecimal however, is base 16. It utilizes our numbering system:
0 1 2 3 4 5 6 7 8 9
Along with an extra 6 digits, A, B, C, D, E, F. Which now becomes:

0 1 2 3 4 5 6 7 8 9 A B C D E F
So A is equal to 10, and F is equal to 15. So now, we can go up to the number 15 without having to add another digit to in front. This was important way back in the early years of computers. You know, when people were psyched about the new, top of the line, 5 megabyte hard drives? Well, each character takes up about 1 byte of space, so every measure was taken to preserve as much memory as possible.

Enough of history. So if you're like me, and you really don't feel like memorizing every hexadecimal number up to 1000 or so, then you'll want a uniform, mathematical system that can convert between any of the three numbers: Binary, Hexadecimal, and Decimal. Albeit this takes work(not too much though. :DDD ), it will give you a greater understanding of binary and hexadecimal and how they work. I know when I learned how to do this, it helped me a lot.

~Decimal to Hexadecimal:
Takes your decimal number, say, 241, and divide by 16 until it equals 0.

241/16= 15 R:1
15/16= 0 R:15

Now, 15 is equal to F in hex, so we turn 15 into F, and put the numbers together in backwards order: F1
F1 is 241 in Hexadecimal.

Now, say you have a larger number, such as 3931.

3931/16= 245 R: 11(B)
245/16= 15 R: 5
15/16= 0 R: 15(F)

So put them in backwards order: F5B

~Decimal to Binary:
This is easy, but it takes longer.
First, take our number 241, and divide it by 2.

241/2= 120 R: 1

Now divide by two up until you reach zero. Your final math should look like this:

241/2= 120 R: 1
120/2= 60 R: 0
60/2= 30 R: 0
30/2= 15 R: 0
15/2= 7 R: 1
7/2= 3 R: 1
3/2= 1 R: 1
1/2= 0 R: 1


Now, take the remainders (R) and write them going from down to up.
So 241 in Binary is:
11110001
Now, notice how you divide by two, and the system is called binary? Emphasis on the "bi", meaning two. Same with Hexadecimal. "dec" means 10, and "hex" means 6, so added together is 16, so you divide by 16 instead.

~Binary to Decimal:
Converting from Binary to Decimal, is a tad bit more difficult, but not too much.
First, you must look at the length of the sequence. We have:

11110001

Ok, so 8 numbers. Not too difficult. How the conversion works is a bit like an algebraic formula, where:

y=2^x

So you take position 0, which is the first in the sequence, and plug it in for x. So:

y=2^0
y=1


Now, do the same for position 1

y=2^1
y=2


Keep going for all positions, until it looks like this:

y=2^0 =1 <--Another way to do this, is to multiply each successive
y=2^1 =2 position by two. So 16*2=32, 32*2=64, and so on.
y=2^2 =4
y=2^3 =8
y=2^4 =16
y=2^5 =32
y=2^6 =64
y=2^7 =128


Next, you look at which numbers in the sequence(11110001) are zero(going backwards):

y=2^0 =1 {1}
|<y=2^1 =2>| {0}
|<y=2^2 =4>| {0}
|<y=2^3 =8>| {0}
y=2^4 =16 {1}
y=2^5 =32 {1}
y=2^6 =64 {1}
y=2^7 =128 {1}

...and you take them out!

y=2^0 =1
y=2^4 =16
y=2^5 =32
y=2^6 =64
y=2^7 =128

Now take the remaining numbers, and add them up!

1+16+32+64+128=241

^This seems complicated at first, but once you understand, you can do it much faster.

~Hexadecimal to Decimal
It's similar to the binary to decimal, in that it requires an algebraic equation, which is:

z*(16^v)

Now, z is equal to the successively backwards number in the number sequence that makes up our hex number. Confused? Try this:
F5B

B*(16^v)
5*(16^v)
F*(16^v)
v is the step in which you are on (beginning with 0) so that:
B*(16^0) = 11
5*(16^1) = 80
F*(16^2) = 3840

Now add up:

11+80+3840= 3931

~Hexadecimal to Binary
There are two ways. One requires memorization(blech) but is faster, and the other uses math but is slower.

1.
Let's convert F1.
F is equal to 15, which is 1111 in binary.
1 is, well, 0001, in binary.
Code:
*NOTE* It works best if each individual digit is in a group of four
when in binary. So if your digit is not in a group of four, just add 
0's at the front until it is.
Now, you simply just put the two together in order
11110001
There, that easy and quick.

2.
If you hate memorization like me, then you will choose this much easier way.
We'll use F1 again. Here, you convert each digit individually instead of as 1 unit. Now, F is equal to 15, which we convert to binary:

15/2 = 7 R: 1
7/2 = 3 R: 1
3/2 = 1 R: 1
1/2 = 0 R: 1

So we have 1111.

1/2= 0 R: 1

So we have 0001 (add the zeros at the front to get it into a grouping of four)

Combine the two:
11110001

It works the same if you are going backwards as well.

Here Are some converters to check your math.
http://home2.paulschou.net/tools/xlate/
http://www.mathsisfun.com/binary-dec...converter.html


I apologize for any mistakes. Please let me know of any, there is a lot of room for some as I said before, I wrote this as a FaceBook comment.
__________________
"Do it, for teh lulz." ~ Proverb

Want custom made, realistic, real-life props? Check out my site.

Last edited by scarecrow; 09-03-2010 at 12:22 AM.
scarecrow is offline   Reply With Quote