Four types of base conversion (with java implementation code)

Hexadecimal conversion

<1>.Convert decimal number to binary number

To convert a decimal number into a binary number, you can use the division by 2 method. Specific steps are as follows:

  1. Keep dividing the decimal number by 2 and record the remainder until the quotient is 0.
  2. Read in reverse direction and the recorded remainder is the binary representation corresponding to the decimal number.

Let me illustrate this process with an example: Convert the decimal number 26 to a binary number.

step 1:

26 / 2 = 13 ... 0
13 / 2 = 6 ... 1
6 / 2 = 3 ... 0
3 / 2 = 1 ... 1
1 / 2 = 0 ... 1

Step 2:
Read the remainder in reverse: 11010

Therefore, the decimal number 26 converts to the binary number 11010.

In actual programming, you can use a loop to perform this process until the quotient is 0, save the remainder each time, and finally reverse them to get the binary representation.

public class DecimalToBinary {<!-- -->
    public static void main(String[] args) {<!-- -->

        int decimalNumber = 26; // Decimal number to be converted
        int binaryNumber = 0; // used to save binary results
        int base = 1; // used to record bit weights

        while (decimalNumber > 0) {<!-- -->
            int remainder = decimalNumber % 2; // Find the remainder
            binaryNumber + = remainder * base; // Add remainder to binary representation
            decimalNumber /= 2; // update quotient
            base *= 10; // Update bit weight value
        }

        System.out.println("Binary representation: " + binaryNumber);
    }
}

<2>Octal to binary

The method of converting an octal number to a binary number is similar to the method of converting a decimal number to a binary number, also using the division by 2 method. Here is a specific step:

  1. First, convert each octal number into its corresponding three-digit binary number (because 8 = 2^3).
  2. Then join these three-digit binary numbers to get the corresponding binary number.

Let me illustrate this process with an example: Convert the octal number 356 to binary.

First, we convert each octal number into the corresponding three-digit binary number:

3 -> 011
5 -> 101
6 -> 110

Then these three-digit binary numbers are concatenated, and the resulting binary number is 011101110.

In actual programming, you can use similar methods to perform this conversion. First, convert each octal number into the corresponding three-digit binary number, and then concatenate them to get the final binary representation.

public class OctalToBinary {<!-- -->
    public static void main(String[] args) {<!-- -->
        int octalNumber = 356; // Octal number to be converted
        String binaryNumber = ""; // Used to save binary results

        // Convert each octal number into the corresponding three-digit binary number
        String[] octalToBinary = {<!-- -->"000", "001", "010", "011", "100", "101", \ "110", "111"};

        //Convert each octal number into the corresponding three-digit binary number, and then concatenate them together
        while (octalNumber != 0) {<!-- -->
            int digit = octalNumber % 10;
            binaryNumber = octalToBinary[digit] + binaryNumber;
            octalNumber /= 10;
        }

        System.out.println("Binary representation: " + binaryNumber);
    }
}

<3>Hexadecimal to binary

To convert a hexadecimal number to binary, you can follow these steps:

  1. Converteach digit in a hexadecimal number into the corresponding four-digit binary number.
  2. Convert each hexadecimal number into the corresponding four-digit binary number, and splice them together in turn to obtain the final binary representation.

For example, assuming we have a hexadecimal number 1A7, we can follow these steps to convert it to a binary number:

  1. Convert a hexadecimal number to a four-digit binary number:

    • The corresponding binary number of 1 is 0001
    • The corresponding binary number of A is 1010
    • The corresponding binary number of 7 is 0111
  2. Splice together the four binary digits corresponding to each hexadecimal number:

    • 1A7 is converted into a binary number as 000110100111

Therefore, the hexadecimal representation of 1A7 corresponds to the binary representation of 000110100111.

If you need to make multiple conversions or want to use a computer program to do it, you can write a simple program to automate the process. Different programming languages may have different ways of handling this conversion, but the basic idea is similar: convert a hexadecimal number into its corresponding four-digit binary number, and then splice them together.

public class HexToBinary {<!-- -->
    public static void main(String[] args) {<!-- -->
        String hexNumber = "1A7"; // The hexadecimal number to be converted
        String binaryNumber = "";

        // Convert each hexadecimal number into the corresponding four-digit binary number
        String[] hexToBinary = {<!-- -->"0000", "0001", "0010", "0011", "0100", "0101", \ "0110", "0111",
                "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};

        //Convert each hexadecimal number into the corresponding four-digit binary number, and then concatenate them together
        for (int i = 0; i < hexNumber.length(); i + + ) {<!-- -->
            char digit = hexNumber.charAt(i);
            if (digit >= '0' & amp; & amp; digit <= '9') {<!-- -->
                binaryNumber + = hexToBinary[digit - '0'];
            } else if (digit >= 'A' & amp; & amp; digit <= 'F') {<!-- -->
                binaryNumber + = hexToBinary[digit - 'A' + 10];
            } else {<!-- -->
                System.out.println("Invalid hexadecimal number");
                return;
            }
        }

        System.out.println("Binary representation: " + binaryNumber);
    }
}

<4>Binary to Octal

Converting a binary number to an octal number can be done by following these steps:

  1. Binary numbers are grouped into groups of 3 digits. If the number of digits is not divisible by 3, 0 is added to the highest bit. ***For example, to convert 1101101 to octal, first fill in the highest bit with 0 to get 011 011 101.

  2. Then,convert each group of three-digit binary numbers into the corresponding octal number.

  3. Finally, arrange each group of converted octal numbers in sequence to obtain the final octal number.

For example, we convert the binary number “1101101” to octal:

First, add 0 to the highest bit to get 011 011 101.

Then, convert each group of three-digit binary numbers into the corresponding octal numbers:

  • 011 -> 3
  • 011 -> 3
  • 101 -> 5

Finally, the converted octal numbers are arranged in order to obtain the final octal number “335”.

<5>Convert decimal to octal

Converting a decimal number to an octal number can be achieved by continuously dividing the decimal number by 8 and then recording the remainder. Specific steps are as follows:

  1. Keep dividing the given decimal number by 8 and record the quotient and remainder each time.
  2. Arrange the obtained remainders in reverse order to obtain the corresponding octal number.

For example, let’s say we want to convert the decimal number 123 to octal:

  • Step 1: Divide 123 by 8, the quotient is 15, and the remainder is 3.
  • Step 2: Divide 15 by 8, the quotient is 1, and the remainder is 7.
  • Step 3: Divide 1 by 8, the quotient is 0, and the remainder is 1.

Then arrange the remainders in reverse order, and the result is 173, that is, the decimal number 123 corresponds to the octal number 173.

Sample code:

public class DecimalToOctal {<!-- -->
    public static void main(String[] args) {<!-- -->
        int decimalNumber = 123;
        int quotient;
        String octalNumber = "";

        quotient = decimalNumber;

        while (quotient > 0) {<!-- -->
            int remainder = quotient % 8;
            octalNumber = remainder + octalNumber;
            quotient = quotient / 8;
        }

        System.out.println("Octal representation: " + octalNumber);
    }
}

In this example, we use a while loop to continuously divide decimal numbers, record the remainder each time, and then concatenate the remainders to get the final octal representation.

<6.>Hexadecimal to Octal

Converting a hexadecimal number to octal involves two steps:

  1. First convert the hexadecimal number to binary number.
  2. The resultingbinary number is then converted into an octal number.

Here’s an example, assuming we want to convert the hexadecimal number 2F6 to octal:

Step 1: Convert hexadecimal number to binary number

  • Convert each digit of the hexadecimal number into the corresponding four-digit binary number. For example, 2 corresponds to the binary number 0010, F corresponds to the binary number 1111, and 6 corresponds to the binary number 0110.
  • Splicing the resulting binary numbers together gives us 001011110110.

Step 2: Convert the resulting binary number to octal number

  • Group the obtainedbinary numbers into three digits from right to left. If there are less than three digits, add 0 to the left. According to this rule, 001011110110 can be divided into 001 011 110 110.
  • Convert each set of binary numbers to the corresponding octal number to get 1366.

Therefore, the hexadecimal number 2F6 corresponds to the octal number 1366.

In actual programming, you can write a program to complete the above two steps, first convert the hexadecimal number into a binary number, and then convert the resulting binary number into an octal number.

public class HexToOctal {<!-- -->
    public static void main(String[] args) {<!-- -->
        String hexNumber = "2F6"; // The hexadecimal number to be converted

        //Convert hexadecimal number to binary number
        String binaryNumber = new java.math.BigInteger(hexNumber, 16).toString(2); // Use the BigInteger class to convert hexadecimal numbers to binary numbers

        //Complete to multiples of 3, because every three binary digits corresponds to one octal digit
        while (binaryNumber.length() % 3 != 0) {<!-- -->
            binaryNumber = "0" + binaryNumber; // Add 0 in front of the binary number until the length is a multiple of 3
        }

        //Convert binary number to octal number
        String octalNumber = new java.math.BigInteger(binaryNumber, 2).toString(8); // Use the BigInteger class to convert binary numbers to octal numbers

        System.out.println("Octal representation: " + octalNumber); // Output the converted octal number
    }
}

<7>Binary to decimal

The method of converting binary to decimal is to multiply the value in each bit by the corresponding weight and then add them together. For example, for an eight-bit binary number 1101101:

 1 1 0 1 1 0 1
   2^6 2^5 2^4 2^3 2^2 2^1 2^0

Multiply the value in each bit by the corresponding weight and add them up:

1*2^6 + 1*2^5 + 0*2^4 + 1*2^3 + 1*2^2 + 0*2^1 + 1*2^0 = 109

So, the binary number 1101101 converted to decimal is 109.

Here is a sample code:

public class BinaryToDecimal {<!-- -->
    public static void main(String[] args) {<!-- -->
        String binaryNumber = "1101101"; // The binary number to be converted
        int decimalNumber = binaryToDecimal(binaryNumber);
        System.out.println("Decimal representation: " + decimalNumber); // Output the converted decimal number
    }

    // This method accepts a binary string as input and returns the corresponding decimal number
    public static int binaryToDecimal(String binaryNumber) {<!-- -->
        int decimal = 0;
        int power = 0; // Used to represent the weight of the current bit, gradually increasing from right to left

        // Starting from the rightmost binary number, process each bit in sequence
        for (int i = binaryNumber.length() - 1; i >= 0; i--) {<!-- -->
            if (binaryNumber.charAt(i) == '1') {<!-- -->
                decimal + = Math.pow(2, power); // If the current bit is 1, add the corresponding weight to the result
            }
            power + + ; // update weights
        }

        return decimal; // Returns the final decimal representation
    }
}

In this code, we loop through each bit of the binary number and, if the current bit is 1, add the corresponding weight to the result. Finally, the accumulated decimal result is returned.

<8>Octal to decimal

To convert an octal number to a decimal number, you can follow these steps:

  1. Determine the weight represented by each digit of the octal number. Counting from right to left, the first weight is 1, the second is 8, the third is 64, and so on.

  2. Multiply each digit of the octal number by the corresponding weight, and then add these products to get the value of the decimal number.

For example, let’s say we have an octal number “123” and want to convert it to decimal:

First, determine the weight of each bit:

  • The first digit is 3, corresponding to weight 1
  • The second bit is 2, corresponding to the weight 8
  • The third bit is 1, corresponding to the weight 64

Then, calculate according to the principle of multiplying and adding the weights:

1 * 64 + 2 * 8 + 3 * 1 = 64 + 16 + 3 = 83

Therefore, the octal number “123” corresponds to the decimal number 83.

Converting an octal number to a decimal number can be achieved through the parseInt method of the Integer class in Java. Here is a simple example code:

public class OctalToDecimal {<!-- -->
    public static void main(String[] args) {<!-- -->
        String octalNumber = "123"; // Octal number to be converted

        //Convert octal number to decimal number
        int decimalNumber = Integer.parseInt(octalNumber, 8);

        System.out.println("Decimal representation: " + decimalNumber); // Output the converted decimal number
    }
}

In this example, we use the static method parseInt(String s, int radix) of the Integer class to convert an octal number to a decimal number. Among them, s is the string to be converted, radix is the base of the string representation, here 8 represents octal.

<9>Hexadecimal to decimal

To convert a hexadecimal number to a decimal number, you can follow these steps:

  1. Determine the weight represented by each digit of the hexadecimal number. ***Counting from right to left, the first weight is 1, the second is 16, the third is 256, and so on.

  2. Multiply each digit ofthe hexadecimal number by the corresponding weight, and then add these products to get the value of the decimal number.

For example, let’s say we have a hexadecimal number “1A3” and want to convert it to a decimal number:

First, determine the weight of each bit:

  • The first digit is 3, corresponding to weight 1
  • The second digit is A (10 in decimal notation), corresponding to the weight 16
  • The third bit is 1, corresponding to the weight 256

Then, calculate according to the principle of multiplying and adding the weights:

1 * 256 + 10 * 16 + 3 * 1 = 256 + 160 + 3 = 419

Therefore, the hexadecimal number “1A3” corresponds to the decimal number 419.

public class HexToDecimal {<!-- -->
    public static void main(String[] args) {<!-- -->
        String hexNumber = "1A3"; // The hexadecimal number to be converted

        //Convert hexadecimal number to decimal number
        int decimalNumber = Integer.parseInt(hexNumber, 16);

        System.out.println("Decimal representation: " + decimalNumber); // Output the converted decimal number
    }
}

<10>Decimal to hexadecimal

Converting a decimal number to hexadecimal can be done by following these steps:

  1. Finds the remainder of a hexadecimal value until the quotient is 0.
  2. Arrange the remainders obtained in each step in reverse order, which is the target hexadecimal number.

For example, let’s convert the decimal number 305:

Step 1: 305 ÷ 16 = 19 more than 1
Step 2: 19 ÷ 16 = 1 + 3
Step 3: 1 ÷ 16 = 0 remainder 1

Arrange the remainders obtained in each step in reverse order, and the result obtained is the hexadecimal number corresponding to the decimal number 305, that is, “131”.

<11>Octal to hexadecimal

When you need to convert a binary number to hexadecimal, you can follow these steps:

  1. Firstconvert the binary number to its corresponding decimal number.
  2. The resulting decimal number is then converted to a hexadecimal representation.

<12>Binary to hexadecimal

When you need to convert an octal number to hexadecimal, you can follow these steps:

  1. Firstconvert the octal number into the corresponding decimal number.
  2. The resulting decimal number is then converted to a hexadecimal representation.

Specifically, these transformations can be accomplished using built-in functions in the programming language or manual algorithms. For example, in Java, you can use Integer.parseInt to convert binary and octal strings to their decimal counterparts, and then use Integer.toHexString to convert them to hexadecimal Control string.