SoFunction
Updated on 2025-04-29

Detailed explanation of the binary conversion and encoding mechanism in Java

In the field of Java programming, phase conversion is an extremely basic and important skill. Whether it is processing the underlying binary data, or performing efficient storage and operation of numbers, understanding various binary and their conversion methods, as well as the concepts of complement and inverse codes, it is essential. Below, we will explore this topic in depth.

1. Overview of common binary

1. Binary

Binary is a counting system based on 2, using only two numbers 0 and 1. At the bottom of the computer, the storage and transmission of data are carried out in binary form, because the computer's hardware circuits can easily realize the representation of two states, such as on and off, high and low levels. For example, the binary number 101 is represented as 1×22+0×21+1×20=5.

2. Octal

The octal system takes 8 as the base and uses eight numbers 0 - 7. Octal was more commonly used in early computer systems. It can easily convert with binary and simplify the representation of binary data. For example, the octal number 37 is expressed as 3×81+7×80=31.

3. Decimal

Decimal is the most commonly used counting system in our daily life. It uses 10 as the base and uses the ten numbers 0 - 9. In Java programs, numeric constants are represented in decimal by default.

4. Hexadecimal

Hexadecimal is based on 16 and uses 0 - 9 and A - F (or a - f) to represent 0 - 15. In computer programming, hexadecimal is often used to represent memory addresses, color values, etc., and the conversion with binary is also very convenient. For example, the hexadecimal number A3 is represented as 10×161+3×160=163.

2. Calculation conversion in Java

1. Decimal to other divisions

In Java, you can useIntegerStatic methods of classestoBinaryString()toOctalString()andtoHexString()Convert decimal integers to binary, octal, and hexadecimal strings.

public class DecimalToOther {
    public static void main(String[] args) {
        int decimal = 25;
        ("Decimal" + decimal + " Convert to binary: " + (decimal));
        ("Decimal" + decimal + " Convert to octal: " + (decimal));
        ("Decimal" + decimal + " Convert to hexadecimal: " + (decimal));
    }
}

2. Other Category to Decimal

passIntegerClassicparseInt()Method and specify the radix, you can convert other binary strings into decimal integers.

public class OtherToDecimal {
    public static void main(String[] args) {
        String binary = "11001";
        String octal = "31";
        String hexadecimal = "19";
        ("Binary" + binary + " Convert to decimal: " + (binary, 2));
        ("Octal" + octal + " Convert to decimal: " + (octal, 8));
        ("hexadecimal " + hexadecimal + " Convert to decimal: " + (hexadecimal, 16));
    }
}

3. Binary, octal and hexadecimal conversion

Binary to octal

You can divide binary numbers from right to left and each group is converted into the corresponding octal number.

public class BinaryToOctal {
    public static void main(String[] args) {
        String binary = "11001";
        int decimal = (binary, 2);
        String octal = (decimal);
        ("Binary" + binary + " Convert to octal: " + octal);
    }
}

Octal to binary

First convert the octal number to a decimal number, and then convert the decimal number to a binary number.

public class OctalToBinary {
    public static void main(String[] args) {
        String octal = "31";
        int decimal = (octal, 8);
        String binary = (decimal);
        ("Octal" + octal + " Convert to binary: " + binary);
    }
}

Binary to hexadecimal

Divide binary numbers from right to left and divide each four-digit group, and then convert each group into the corresponding hexadecimal number.

public class BinaryToHexadecimal {
    public static void main(String[] args) {
        String binary = "11001";
        int decimal = (binary, 2);
        String hexadecimal = (decimal);
        ("Binary" + binary + " Convert to hexadecimal: " + hexadecimal);
    }
}

Hexadecimal to binary

First convert the hexadecimal number to a decimal number, and then convert the decimal number to a binary number.

public class HexadecimalToBinary {
    public static void main(String[] args) {
        String hexadecimal = "19";
        int decimal = (hexadecimal, 16);
        String binary = (decimal);
        ("hexadecimal " + hexadecimal + " Convert to binary: " + binary);
    }
}

3. Complement and reverse code

1. Inverse code (One's Complement)

In Java, inverse code is used to represent negative numbers. For positive numbers, the inverse code is the same as the original code; for negative numbers, the inverse code is based on the original code, the sign bit (highest bit) remains unchanged, and the rest are inverse. However, Java itself does not have a built-in method to directly obtain the inverse code, but it can be implemented through custom methods.

public class OnesComplement {
    public static String getOnesComplement(int number) {
        if (number >= 0) {
            return (number);
        } else {
            String binary = ((number));
            StringBuilder onesComplement = new StringBuilder();
            for (char bit : ()) {
                (bit == '0'? '1' : '0');
            }
            return "1" + ();
        }
    }
    public static void main(String[] args) {
        int num = -5;
        (num + "The inverse code: " + getOnesComplement(num));
    }
}

2. Two's Complement

Complement code is also a coding method used to represent negative numbers and is widely used in Java. For positive numbers, their complement is the same as the original code; for negative numbers, their complement is based on the inverse code, and the last position is added 1. Similarly, Java does not have a built-in method to directly obtain complement code, but it can be implemented in the following ways.

public class TwosComplement {
    public static String getOnesComplement(int number) {
        if (number >= 0) {
            return (number);
        } else {
            String binary = ((number));
            StringBuilder onesComplement = new StringBuilder();
            for (char bit : ()) {
                (bit == '0'? '1' : '0');
            }
            return "1" + ();
        }
    }
    public static String getTwosComplement(int number) {
        if (number >= 0) {
            return (number);
        } else {
            String onesComplement = getOnesComplement(number);
            StringBuilder twosComplement = new StringBuilder(onesComplement);
            for (int i = () - 1; i >= 0; i--) {
                if ((i) == '0') {
                    (i, '1');
                    return ();
                } else {
                    (i, '0');
                }
            }
            return "1" + ();
        }
    }
    public static void main(String[] args) {
        int num = -5;
        (num + "'s complement: " + getTwosComplement(num));
    }
}

The above article adds more code examples for binary conversion, covering the mutual conversion between binary and octal and hexadecimal, and adds the complement and inverse parts.mainMethods are used for testing. Hope this fits your needs.

This is the article about the basis conversion and coding mechanism in Java. For more related contents of Java basis conversion and coding mechanism, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!