convert binary numbers to decimal numbers code example
Example 1: binary to decimal
import java.util.Scanner;
public class BinaryToDecimal {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String binary = sc.next();
System.out.println(Integer.parseInt(binary,2));
int n=0,dec=0;
for(int i=binary.length()-1;i>=0;i--)
{
dec = dec + Integer.parseInt(String.valueOf(binary.charAt(i)))*(int)Math.pow(2,n);
n++;
}
System.out.println(dec);
}
}
Example 2: binary to decimal
= (11001010)2
= 1x27+1x26+0x25+0x24+1x23+0x22+1x21+0x20
= 128+64+0+0+8+0+2+0
= (202)10