This is the first day of my participation in Gwen Challenge

Conversion of base

  • Base R to base 10 is expanded by weight

By weight expansion, you multiply the number of each digit times your weight.

Such as binary number 100011.011 = 12 ^ ^ 4 + 5 + 02 02 02 ^ ^ 3 + 2 + 12 + 12 ^ ^ 1 02 ^ 0 + + 12 (1) ^ (2) + 1 * 2 ^ (3) = 35.375 such as octal number 12345 = 1 * 8 28 ^ ^ 4 + 3 + 3 * 8 * 8 ^ 1 ^ 2 + 4 + 5 * 8 ^ 0 = 5349 such as hexadecimal number 456 = 4 * 16 * 16 ^ 1 ^ 2 + 5 + 6 * 16 ^ 0 = 1110Copy the code

Do you get it from these examples? Is it very simple, so start to try it yourself, do not overestimate oh.

Now that we know how to convert base R to base 10, how do we convert base 10 to base R?

Use short division

So what is short division? Let’s look at examples again.

1 2 | 2 | 97 more than more than more than 48 0 2 | 24 more than 0 2 | 2 | 12 more than 0 6 0 1 2 | 2 | 3 more than more than has been to take more than 2, 1, 0 until business is 0, then the end result is what? I'm just going to write the remainder the other way around, 1100001 and I'm going to do the same thing with all the other bases just replace the 2 with R.Copy the code

Knowing the reciprocal conversion between base R and base 10, how to convert base 2, base octal and base 16? Of course, it is certainly possible to convert them to base 10 and then to the required base, but it is a lot of trouble, is there any quick way? The answer is yes.

Binary to octal 1001110101 from right to left, three digits, not enough digits left fill 0 namely 001 001110101 -->1 165 namely 1165 binary to hexadecimal 1001101011101 from right to left four digits, 0001 0011 0101 1101 ->1 35D 135DCopy the code

If you want to switch from octal to hexadecimal, you can switch from octal to hexadecimal.

Unfortunately, octal and hexadecimal do not interconvert, so you need to convert to binary.

The above is the conversion of the base system, welcome to leave a message to discuss, if there is no welcome to correct.Copy the code