Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert binary string value to decimal

Tags:

java

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)

like image 968
goldsoft Avatar asked Sep 15 '11 22:09

goldsoft


People also ask

How do you convert a string to a decimal?

Converting a string to a decimal value or decimal equivalent can be done using the Decimal. TryParse() method. It converts the string representation of a number to its decimal equivalent.

How do we convert a binary number value to a decimal value?

To convert a binary integer to decimal, start by adding the left-most digit to 0. Step 2. Next, multiply this by 2, and add the next digit in your number (as you progress from left to right) to this product. (In other words, multiply the current product in each step by 2 and add the current digit).

What is 0.75 binary?

The decimal number 0.75 is written as 0.11 in binary.


1 Answers

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

int decimalValue = Integer.parseInt(c, 2); 
like image 90
Guido Avatar answered Oct 03 '22 23:10

Guido