# Converting Numbers: A Crash Course

I did this in 8th grade. I never thought it would come in useful. I still debate it, but here's how you convert numbers on paper. I am putting this in my journal because someone requested it last week, and I realized I never published this.

First, rethink numbers. In the Hindu-Arabic base 10 system we're used to, we have ten numbers, 0-9. We will call this "decimal" because "dec" means "10." When figuring out how many digits you need to express an amount, you are dealing with powers of 10.

10^0 = 1 (anything ^0 is one)
10^1 = 10
10^2 = 100
10^3 = 1000

You see where this is going, right? The "to the power of" is how many zeros after the one, which uses the number of the power and adds one. Like 1000 (power of 3) is 4 digits. You might think "duh" but it gets a little confusing when you have numbers in a different base than 10. Like octal (0-7, oct means "8"):

8^0 = 1
8^1 = 10 (8 in decimal)
8^2 = 100 (64 in decimal)
8^3 = 1000 (512 in decimal)

Then there is hexadecimal ("six and ten"), which is 0-F where A-F represents 10-15. This is usually called "hex" because programmers hate lots of syllables. I have also expressed these in decimal:

16^0 = 1
16^1 = 10 (16 in decimal)
16^2 = 100 (64 in decimal)
16^3 = 1000 (4096 in decimal)

Binary, I left for last. "Bi" means 2, because there's only 2 digits, 0 and 1:

2^0 = 0
2^1 = 10 (2 in decimal)
2^2 = 100 (4 in decimal)
2^3 = 1000 (8 in decimal)

In reality, there is nothing above a "1" in binary, so the above example should have actually been:

10^0 = 0
10^1 = 10 (2 in decimal)
10^10 = 100 (4 in decimal)
10^11 = 1000 (8 in decimal)

But I worried you might still think of "10" as "ten." Stop thinking that. Well, at least with anything outside of your normal decimal systems. "10" as number can mean different things. It can mean two items in binary, eight items in octal, ten items in decimal, and sixteen items in hex. So, for the purposes of this document, I am going to follow some "literal standards" used in programming when telling which numbers go with what.

0b10 = 1 and 0 in binary, or 2 in decimal
0o10 = 1 and 0 in octal, or 8 in decimal
0x10 = 1 and 0 in hexadecimal, or 16 in decimal

If I have nothing as a prefix, it means decimal. So from binary to hex:

0b101010 = 0o52 = 42 = 0x2A

So why have those numbers at all? Well, binary is how a computer thinks. When it all comes down to it, computers only think like an electrical switch: it's either on (1) or off (0), often called "bits." Octal was used more in the old days of computers, where 2^3 in decimal represents a byte (8 bits), but you still see it in Linux as permission labels, like 0755. Hex is 2 bytes, or 16 bits, and still used in a lot of computer math and labels when addressing memory.

For the purposes of this document, I will only talk about binary and hex. Octal is for your own study, as it is used a lot less.

Converting Decimal to Binary:
In any binary, I want you to memorize the following "magic binary number set":

1, 2, 4, 8, 16, 32, 64, 128, 256, 512, and 1024.

As you can see, they are doubles of the previous number, and are 2^0, 2^1, 2^2, and so on...

So take a decimal number like 13. We need to take a number that is the highest of that set that is less than or equal to 13. In this case, it is 8. 8 is 2^3, so we know we have 4 decimal digits because we think of "0" as the first place (0 - 3 is four digits or places).

0b 1???

We subtract 8 from 13, which leaves us with 5. We go one down from 8 (2^3) and find what goes in the third place (2^2). Is 4 less than 5? It is!

0b11??

We now have accounted for that, so we subtract 4 from 5, leaving us with 1. So now we go down to the second place (2^1). Is 2 less than 1? No. But we have to leave a placeholder to show we already covered that.

0b110?

We don't subtract anything. We just go down to 2^0, first place, and that's 1. We have 1 left! We have to account for that.

0b1101

So there you have it! Let's take a slightly longer number like 42. Since we see that 32 is less than 42 (64 is too high), and we know that's 2^5, we have 6 places.

1 : 42 - 32 = 10
0 : 16 > 10
1 : 10 - 8 = 2
0 : 4 > 2
1 : 2 - 2 = 0 (which means the rest will all be zeros)
0 : [see above]

So 42 = 0b101010

Binary to Decimal
Much easier. You have that magical binary set memorized, right? If you know how many digits you have, you know where to start. Let's start with:

0b11001

1 : 2^4 = 16
1 : 2^3 = 8
0 : 2^2 = 0
0 : 2^1 = 0
1 : 2^0 = 1

So add them together 16 + 8 + 1 = 25 decimal.

Decimal to Hex
This seems harder because you have "extra numbers" like A - F. Here's where thinking in symbols versus numbers comes in handy. 0-F are just 16 symbols that denote amounts. You're used to 10 meaning ten items, but in hex, 0x10 is sixteen items. That's another reason 0x### comes in handy so your brain does not get tricked.

Let's take 42 again. Like any other number, they are in powers. The "magic hexadecimal set" I want you to remember is:

1, 16, 64, 512, and 4096

Again, you find the highest number that is not greater than yours. That would be 16. Now you figure out how many 16s go in 42 with a remainder, and follow the same pattern as you did with binary. Since 16 = 16^1, we know it's in the second place, because again, ^0 is always first.

[3 x 16 = 48, too high]
2 x 16 = 32, so 42 - 32 = 10

0x2?

So now we have 10. We know 10 < 16, so there's probably a symbol/number attached to it, which is A.

42 = 0x2A

Viola!

Hex to Decimal
Again, fairly easy now that you know the pattern. Just like binary, you know what place everything has. Let's take a number like 0x3FF

3 : 16^2 * 3 = 256 * 3 = 768
F : 16^1 * 15 = 16 * 15 = 240
F : 16^0 * 15 = 1 * 15 = 15

768 + 240 + 15 = 1023
• Post a new comment

#### Error

Anonymous comments are disabled in this journal

default userpic