Unleash the Power of Java: Converting Binary and Decimal Numbers

Understanding Binary and Decimal Numbers

When working with numbers in programming, it’s essential to understand the basics of binary and decimal systems. Binary numbers consist only of two digits: 0 and 1, expressed in the base 2 numeral system. On the other hand, decimal numbers use 10 digits: 0 to 9, expressed in the base 10 numeral system.

Converting Binary to Decimal: Two Approaches

In this article, we’ll explore two methods to convert binary numbers to decimal using Java. The first approach uses a custom method, while the second leverages the built-in parseInt() method of the Integer class.

Custom Method: Binary to Decimal Conversion

Our custom method takes a binary string as input and returns its decimal equivalent. Here’s the code:
“`
public class BinaryToDecimal {
public static int binaryToDecimal(String binary) {
int decimal = 0;
int power = 0;
for (int i = binary.length() – 1; i >= 0; i–) {
if (binary.charAt(i) == ‘1’) {
decimal += Math.pow(2, power);
}
power++;
}
return decimal;
}

public static void main(String[] args) {
    String binary = "1010";
    int decimal = binaryToDecimal(binary);
    System.out.println("Binary " + binary + " is equal to decimal " + decimal);
}

}

**Using
parseInt()`: Binary to Decimal Conversion**

Alternatively, we can use the parseInt() method to achieve the same result. This method takes a string and a radix (base) as arguments, returning the parsed integer value.

public class BinaryToDecimal {
public static void main(String[] args) {
String binary = "1010";
int decimal = Integer.parseInt(binary, 2);
System.out.println("Binary " + binary + " is equal to decimal " + decimal);
}
}

Converting Decimal to Binary: Two Approaches

Now, let’s explore two methods to convert decimal numbers to binary using Java. The first approach uses a custom method, while the second leverages the built-in toBinaryString() method of the Integer class.

Custom Method: Decimal to Binary Conversion

Our custom method takes a decimal integer as input and returns its binary equivalent. Here’s the code:
“`
public class DecimalToBinary {
public static String decimalToBinary(int decimal) {
String binary = “”;
while (decimal > 0) {
int remainder = decimal % 2;
binary = (remainder == 0? “0” : “1”) + binary;
decimal /= 2;
}
return binary;
}

public static void main(String[] args) {
    int decimal = 10;
    String binary = decimalToBinary(decimal);
    System.out.println("Decimal " + decimal + " is equal to binary " + binary);
}

}

**Using
toBinaryString()`: Decimal to Binary Conversion**

Alternatively, we can use the toBinaryString() method to achieve the same result. This method takes an integer argument and returns the string representation of the number in base 2 (binary).

public class DecimalToBinary {
public static void main(String[] args) {
int decimal = 10;
String binary = Integer.toBinaryString(decimal);
System.out.println("Decimal " + decimal + " is equal to binary " + binary);
}
}

By mastering these conversion techniques, you’ll be well-equipped to tackle a wide range of programming challenges in Java. Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *