Convert binary to decimal in Java
In this post, you will learn how to convert a binary into an equivalent decimal number using Java programming language. Here, we have mentioned two ways to convert a binary to a decimal value.
A binary number consists of two numbers 0s and 1s. It is expressed in the base-2 numeral system or binary numeral system. As the computer only understands binary language that is 0 or 1, all inputs given to a computer are decoded by it into series of 0's or 1's to process it further. Any combination of 0 and 1 is a binary number such as 1011, 100, 101011, 111011 etc.
A decimal number is a base-10 number system. It ranges from 0 to 9 i.e., 0,1,2,3,4,5,6,7,8,9. Any combination of digits is a decimal number such as 61, 22, 912, 0, 5 etc. The main advantages of the decimal number system are easy-to-read, used by humans and easy to manipulate.
Java convert decimal to binary using Integer.parseInt() function
Java provides a built-in Integer.parseInt() function to convert various values to int with a given redix. This function returns a decimal value. Here is a simple example of converting binary to decimal in java.
// Java program to convert binary to decimal
public class ConvertBinaryToDecimal{
public static void main(String args[]){
System.out.println("Binary value of 1010 : "+Integer.parseInt("1010", 2));
System.out.println("Binary value of 1101 : "+Integer.parseInt("1101", 2));
System.out.println("Binary value of 1111 : "+Integer.parseInt("1111", 2));
System.out.println("Binary value of 1001 : "+Integer.parseInt("1001", 2));
System.out.println("Binary value of 1100 : "+Integer.parseInt("1100", 2));
}
}
Output of the above code:
Binary value of 1010 : 10
Binary value of 1101 : 13
Binary value of 1111 : 15
Binary value of 1001 : 9
Binary value of 1100 : 12
Java convert binary to decimal using loop
Here, we have applied the custom logic using a loop to convert binary to decimal in Java.
public class ConvertBinaryToDecimal {
// defining function to convert binary to decimal
static int binaryToDecimal(int n)
{
int num = n;
int decimal = 0;
// Initializing base value to 1
int base = 1;
int temp = num;
while (temp > 0) {
int last_digit = temp % 10;
temp = temp / 10;
decimal += last_digit * base;
base = base * 2;
}
return decimal;
}
// Driver Code
public static void main(String[] args)
{
System.out.println("Decimal representation of 11001: "+binaryToDecimal(11001));
System.out.println("\nDecimal representation of 10011: "+binaryToDecimal(10011));
System.out.println("\nDecimal representation of 1001: "+binaryToDecimal(1001));
System.out.println("\nDecimal representation of 1101: "+binaryToDecimal(1101));
}
}
Output of the above code:
Decimal representation of 11001: 25
Decimal representation of 10011: 19
Decimal representation of 1001: 9
Decimal representation of 1101: 13
Java convert decimal to binary using custom method
In the given Java program, we have used the custom method to convert decimal to binary.
public class Main {
public static void main(String[] args) {
// binary number
long num = 10011;
// calling method
int decimal = convertBinaryToDecimal(num);
System.out.println("Binary to Decimal");
System.out.println(num + " = " + decimal);
}
public static int convertBinaryToDecimal(long num) {
int decimalNum = 0, x = 0;
long remainder;
while (num != 0) {
remainder = num % 10;
num /= 10;
decimalNum += remainder * Math.pow(2, x);
++x;
}
return decimalNum;
}
}
Output of the above code:
Binary to Decimal
10011 = 19
Related Articles
Sort array in ascending order Javanth prime number in Java
Determinant of a matrix in Java
Find the greatest of three numbers in Java
Java random number between 1 and 10
Pascal triangle program in Java
Difference between vector and arraylist in Java
Capitalize first letter of each word Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java
Check whether the given number is even or odd in java
Print prime numbers from 1 to 100 in Java
Java prime number program
Java program to convert celsius to fahrenheit
Fibonacci series program in Java
Java program to check leap year
Java program to find factorial of a number