Write in front: the blogger is a real combat development after training into the cause of the “hill pig”, nickname from the cartoon “Lion King” in “Peng Peng”, always optimistic, positive attitude towards things around. My technical path from Java full stack engineer all the way to big data development, data mining field, now there are small achievements, I would like to share with you what I have learned in the past, I hope to help you on the way of learning. At the same time, the blogger also wants to build a perfect technical library through this attempt. Any anomalies, errors and matters needing attention related to the technical points of the article will be listed at the end, and everyone is welcome to provide materials in various ways.

  • Please criticize any mistakes in the article and revise them in time.
  • If you have any questions you would like to discuss or learn, please contact me at [email protected].
  • The style of the published article varies from column to column, and all are self-contained. Please correct the deficiencies.

How to convert between bases?

Key words: binary, decimal, base conversion, octal, hexadecimal \

The article directories

A, into the system

The first numbers and methods of calculation you encountered were based on the decimal system, so the base system means a counting method. Carry according to the corresponding base rules, the same string of numbers in different bases will also correspond to different sizes, so in the program will have a clear identification of the number base.

1. The decimal

The decimal system is a decimal system in which the decimal digits can only be in the range of 0 to 9, which is the default.

2. The binary

Binary means that every two digits carry one, so the range of digits in each digit can only be 0 or 1, and Java uses 0B to start with.

3. The octal

Octal is one digit in eight, so each digit can only be in the range of 0 to 7. Java uses 0 as the starting point.

4. Hexadecimal

The hexadecimal number is 1. If A number is greater than 10, the number must be represented by the letter A (10), B (11), and F (15). In Java, each digit must be in the range of 0 to F.

Two, decimal and binary conversion

At the beginning of programming, we should always learn the conversion between binary and binary, because this is the base system used in the work of the computer, and many bit operations are also carried out in binary, so we must master binary conversion.

1. Convert binary to decimal

The process of converting a number from another base to a decimal number is actually converting to the corresponding base. Before converting, let’s look at the most familiar decimal notation: 1367. See this number we will not hesitate to say: one thousand three hundred and sixty-seven, this is something we take for granted, but what is the specific process?

  • 1367 = 7 × 1 + 6 × 10 + 3 × 100 + 1 × 1000
  • 1367 = 7 times 10^0 + 6 times 10^1 + 3 times 10^2 + 1 times 10^3

As you can see from the above steps, a number is actually interpreted from right to left, but we are too familiar with the decimal system to ignore this step, so use this number to feel: 1237173927, I guess you must be counting from right to left, from the ones place, what is it? So the same is true for all the other bases, so let’s just read a couple of binary numbers.

  • 101:1 × 2^0 + 0 × 2^1 + 1 × 2^2 = 5
  • 10010:0 × 2^0 + 1 × 2^1 + 0 × 2^2 + 0 × 2^3 + 1 × 2^4 = 18
  • 1010101:1 × 2^0 + 0 × 2^1 + 1 × 2^2 + 0 × 2^3 + 1 × 2^4 + 0 × 2^5 + 1 × 2^6 = 85

Congratulations, our base conversion is complete. The way to do that is to add up the results from right to left. At the same time, we notice that as long as the last digit is 0, the number must be divisible by 2, as it is in other bases (just as a number with the ones digit zero is always divisible by 10).

2. Convert decimal to binary

  • General method:

The process of converting a decimal number to a binary number is actually a process of constantly dividing and recording the remainder. Since we are converting to binary, our divisor is 2. The process is as follows:

Base 10 numbers: 37 -> Binary result: 100101

  • Fast conversion of small numbers:

There is actually a faster way to convert numbers that are not very large, but if we are familiar with the result of 2 to the power, basically remember that 2 to the power of 10 is 1024. The way we do that is we take a decimal number and break it down into the sum of two to the n, starting with the largest number. For example, for 37, the largest binary number is 32, followed by 4, and then 1, so the equation is: 37 = 32 + 4 + 1.

  • Thirty-two is two to the fifth
  • 4 is 2 squared
  • 1 is 2 to the zero

So the corresponding binary number in the sixth position, the third position, the first position is 1 (stagger one bit), and the rest is 0. It’s also a quick way to know where the highest digit is, and it’s very smooth to write the binary from left to right, but it’s not very good for large numbers.