Zophar's Message Domain

Zophar's Message Domain (http://www.zophar.net/forums/index.php)
-   Rom Hack (http://www.zophar.net/forums/forumdisplay.php?f=8)
-   -   Convert Between Decimal, Hex, and Binary (And a Hex Explanation) For Dummies (http://www.zophar.net/forums/showthread.php?t=14509)

scarecrow 09-03-2010 12:16 AM

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.

Reaper man 09-03-2010 03:10 AM

I like guides. Stickied.

scarecrow 09-03-2010 03:33 AM

Oh, tank you. tank you very mucha.

Iconoclast 09-22-2010 11:32 PM

Good compilation, but I just wanted to say that I don't consider the first method you stated of converting from hexadecimal to binary "memorization".

If you understand both bits and nybbles (1 nybble:4 bits = 1:1), then I think it should come naturally fairly easy. 1 bit = 2^1 possibilities, and 4 bits = 1 nybble = 2^4 = 16 possibilities, which is the number of digits that base sixteen "hexadecimal" arithmetic supports.

Given the above, rather than memorizing the first sixteen whole numbers of the base sixteen arithmetic system, it should be pretty easy to quickly derive for yourself what ???? (binary) is in decimal, which tells you the digit in hexadecimal. Like you said, d3(2^3) + d2(2^2) + d1(2^1) + d0(2^0) gives you the conversion, and it doesn't take very long in my experience.

If you agree with what I've said, this should be an easy and efficient method of converting between both from binary to hexadecimal and from hexadecimal to binary. It might be a lot harder for others to learn, so this is more of my opinion than a correction.

scarecrow 10-25-2010 01:14 AM

Forgive me, I'm having a little trouble understanding your conversion method. Could you please explain again?

Iconoclast 11-26-2010 03:00 AM

Of course ... sorry about the month delay in my response.

Before special interest in hacking I have always been very interested in developing my understanding of math, so I'm happy to clarify or discuss.

Quote:

~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.
This is the "memorization" method that you described.

Instead of storing these equivalences in our memory banks, it should be very easy to derive them to the point where we don't memorize them so much as know them.

It's very clear that you're acquainted enough with hexadecimal to quickly say what the decimal value of each digit is.

Instead of memorizing that the hex digit A is, in binary, 1010, we can mentally retrieve that knowledge by thinking:
0xA = 1(2^3) + 0(2^2) + 1(2^1) + 0(2^0) = 8 + 2 = 10.
0x7 = 0(2^3) + 1(2^2) + 1(2^1) + 1(2^0) = 7
et cetera

What I'm saying, is that this mental math is very easy to do efficiently. The speed of doing this for hex digits will be almost as fast as actually memorizing their binary forms, especially with practice, obviously.

I know it looks extensive at first, but with practice, it should occur naturally to you. Note that binary arithmetic is also known as base two arithmetic, so the base of all of those exponents you're multiplying by is the number two. The power of each exponent is the digit place of the binary format.

Pocoryu 01-05-2012 03:13 AM

I'm having trouble understanding the binary to decimal part.
What does the y stand for and what does the x stand for?

Pocoryu 01-05-2012 03:16 AM

I just realised. Sorry to be such a tard.

DarkZio 01-12-2012 04:53 AM

this si why i came here to start taking emulation to the next level, sweet chart ill take a quick study

empresorium 01-29-2012 02:50 PM

Never thought that it would be so simple to convert decimal numbers to Binary Digits. Though i don't have any use for converting decimal to binary numbers but still it is good that i know how it is done.


All times are GMT. The time now is 01:33 PM.

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.