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)
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.
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).
The decimal number 0.75 is written as 0.11 in binary.
Use Integer.parseInt
(see javadoc), that converts your String
to int
using base two:
int decimalValue = Integer.parseInt(c, 2);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With