How to convert binary string value to decimal

How to convert a binary String such as

String c = "110010"; // as binary

to the value in decimal in Java? (expected result in the example is 50)


Use Integer.parseInt (see javadoc), that converts your String to int using base two:

int decimalValue = Integer.parseInt(c, 2);

public static int integerfrmbinary(String str){
    double j=0;
    for(int i=0;i<str.length();i++){
        if(str.charAt(i)== '1'){
         j=j+ Math.pow(2,str.length()-1-i);
     }

    }
    return (int) j;
}

This piece of code I have written manually. You can also use parseInt as mentioned above . This function will give decimal value corresponding to the binary string :)


I think you are looking for Integer.parseInt. The second argument takes a radix, which in this case is 2.

Integer.parseInt(c, 2)

int i = Integer.parseInt(c, 2);