# PentaHex

## Number Bases

When counting, we use the digits 0 to 9, and once those are used up, we add a decimal place and start back up at 0. But what if we only used 4 digits, 0 to 3? Or what if we added a digit, A? By doing so, we would be changing the base of the number system.

Most of the numbers we see are in base 10, which uses the digits 0 to 9. A few other common bases are binary (base 2) which uses 0 and 1, and hexadecimal (base 16) which uses 0 to 9 and A to F.
NOTE: The digit A represents 10, the digit B represents 11, and so on.

Bases are represented with a subscript after the number representing the base. For example, 102 or 12349. Any numbers without this subscript are assumed to be base 10.

Scientific Notation is very useful with number bases. As a reminder, scientific notation of a number like 354 would be 3×102+5×101+4.

The scientific notation of a number changes when the base changes. The scientific notation of 3546 would be 3×62+5×61+4.

An important thing to note, each digit in a number has to be less than its base. 5 can't be a digit in base 4, and 7 can't be a digit in base 7.

Now on to adding numbers of a different base. Essentially, this is the same as adding in base 10. Let's do an example in hexadecimal.

45
+3D
---
84

The 5 and D add to 12. (E, F, 10, 11, 12) The units digit is 2. Then we add 4 + 3 + 1 = 8. Our tens digit is 8. Our answer is 0x82.

### Converting to Base 10

We can start with converting numbers from any base to base 10. This is very simple, just write it in scientific notation, and simplify.

Let's say we want to convert 10102 to base 10.

1. First, we write it in scientific notation: 1×23+0×22+1×21+0.
2. Then, we simplify: 8 + 2 = 10.

We're done! It's as simple as that! You can also think of it as a table:

 Digit 1 0 1 0 Value 8 4 2 1 Sum 8 + 0 + 2 + 0 = 10

### Converting from Base 10

Converting to another base will be a bit more difficult, though. There are a few steps to go through:

1. Divide your number by the base and record the remainder. This is the rightmost digit.
2. Divide the answer from the previous division by the base again. This digit goes to the left of the last one.
3. Keep dividing and recording the remainders until the number is less than the base. This is the leftmost digit.

Essentially, you're just going to divide by the base and get the remainder. Let's convert 144 to base 5.

1. We divide our number by the base: 144 / 5 = 28, remainder 4. ___4
2. 28 / 5 = 5, remainder 3. __34
3. 5 / 5 = 1, remainder 0. _034
4. 1 is less than the base, 5. We use it as the leftmost digit: 1034
5. Our completed conversion is 10345

Notice how since these are all remainders of the base, all the numbers must be less!

### Examples

First, let's convert 0xDFA2 from hexadecimal (base 16) to decimal (base 10).

1. The first digit adds to: D × 163 = 13 × 4096 = 53248
2. The second digit adds to: F × 162 = 15 × 256 = 3840
3. The third digit adds to: A × 1 = 10 × 16 = 160
4. The final digit is just 2 × 1 = 2
5. Adding them all up, we get 53248 + 3840 + 160 + 2 = 57250

Now let's try converting 119 from decimal to base 3.

1. 119 / 3 = 39 R 2
2. 39 / 3 = 13 R 0
3. 13 / 3 = 4 R 1
4. 4 / 3 = 1 R 1
5. 1 / 3 = 0 R 1
6. Our number is 111023

Let's convert 100111002 to base 4 and then to base 16 quickly, using a special trick.

1. For this, let's look at a special property of these bases.

Since 4 is a power of 2 and 16 is a power of 4, we can group these up in sections.
2. For the first part, we will split up 10011100 into groups of 2: 10 01 11 00. We can convert each of these numbers into base 4 individually: 3 1 4 0. Just put these numbers together, 21304, and you have converted it to base 4!
3. For the next part, we spit up 3140 into groups of 2: 21 30. We can convert these to base 16 individually again: 9 C(12). Finally we have our base 16 number, 0x9C.

2 digits, such as 10, in binary can range from 0-3, just like 1 digit in base 4. 2 digits in base 4 can range from 0-15, just like 1 digit in base 16. This new strategy allows us to only have to deal with small numbers. It works for all powers. 2 digits in base 3 is 1 digit in base 9, and 3 digits in base 3 is 1 digit in base 27.

As a final challenge, let's convert 17378 from base 8 to base 29.

We'll need to divide this into 2 parts, converting from base 8 to base 10, then from base 10 to base 29.

1. Let's do this in scientific notation. 1 × 83 + 7 × 82 + 3 × 8 + 7 = 512 + 7 × 64 + 3 × 8 + 7 = 512 + 448 + 24 + 7 = 991
2. Now, to convert to base 27, we need to repeatedly divide by 27:
3. 991 / 27 = 36 R 19
4. 36 / 27 = 1 R 9
5. 1 / 27 = 0 R 1
6. Our number is 19_. To write 19 as a single digit, we could either count up for A=10, B=11, and on, or get the 10th letter in the alphabet since 9 + 10 = 19 (work with me please). Since the 10th letter is J, our number is 19J. Weird!

### Base Converter?

Changing bases is very tedious work, especially with large numbers. There's a lot of times where you want to change a number's base fast. That's why we've created a base converter. It can change any number from one base to another.

Simply enter the number in one base (make sure each of the digits are valid) and enter it's base in the start base spot. Then, enter the base you want to convert it to in the end base spot. It's a helpful tool for large numbers and quick conversions!