How to convert binary string value to decimal
Use Integer.parseInt
(see javadoc), that converts your String
to int
using base two:
int decimalValue = Integer.parseInt(c, 2);
How to convert binary string to decimal?
The parseInt
function converts strings to numbers, and it takes a second argument specifying the base in which the string representation is:
var digit = parseInt(binary, 2);
See it in action.
How to convert a binary String to a decimal string in Java
To convert a base 2 (binary) representation to base 10 (decimal), multiply the value of each bit with 2^(bit position) and sum the values.
e.g. 1011 -> (1 * 2^0) + (1 * 2^1) + (0 * 2^2) + (1 * 2^3) = 1 + 2 + 0 + 8 = 11
Since binary is read from right-to-left (i.e. LSB (least significant bit) is on the rightmost bit and MSB (most-significant-bit) is the leftmost bit), we traverse the string in reverse order.
To get the bit value, subtract '0' from the char. This will subtract the ascii value of the character with the ascii value of '0', giving you the integer value of the bit.
To calculate 2^(bit position), we can keep a count of the bit position, and increment the count on each iteration. Then we can just do 1 << count to obtain the value for 2 ^ (bit position). Alternatively, you could do Math.pow(2, count) as well, but the former is more efficient, since its just a shift-left instruction.
Here's the code that implements the above:
public static int convertBinStrToInt(String binStr) {
int dec = 0, count = 0;
for (int i = binStr.length()-1; i >=0; i--) {
dec += (binStr.charAt(i) - '0') * (1 << count++);
}
return dec;
}
Convert binary string to binary or decimal value
You could use the packBits
function (in the base
package). Bear in mind that this function requires very specific input.
(yy <- intToBits(5))
# [1] 01 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
# [26] 00 00 00 00 00 00 00
# Note that there are 32 bits and the order is reversed from your example
class(yy)
[1] "raw"
packBits(yy, "integer")
# [1] 5
There is also the strtoi
function (also in the base
package):
strtoi("00000001001100110000010110110111", base = 2)
# [1] 20121015
strtoi("000101", base = 2)
# [1] 5
Java converting binary string to decimal
Here is where your code went wrong:
str.charAt(16-1 - i) * Math.pow(2, i);
You just multiplied a char
by a double
. This will evaluate to the ASCII value of char
times the double, not 0 or 1.
You need to convert this to an integer first:
Integer.parseInt(Character.toString(str.charAt(16-1 - i))) * Math.pow(2, i)
Or, you can just:
Integer.parseInt(binaryString, 2)
Related Topics
When Should We Create Our Own Java Exception Classes
Java.Io.Ioexception: Mark/Reset Not Supported
How to Ensure Hashcode() Is Consistent with Equals()
How to Display a Svg Byte Array as an Image in a Jasperreport
JPA and Hibernate - Criteria VS. Jpql or Hql
In What Order Do Static Blocks and Initialization Blocks Execute When Using Inheritance
Case Insensitive String as Hashmap Key
Lambda Expression to Convert Array/List of String to Array/List of Integers
Bigdecimal Equals() Versus Compareto()
How to Store Java Objects in Httpsession
Use Custom Fonts When Creating PDF Using Ireport
MAC Osx - Illegalstateexception: the Driver Is Not Executable:
Authenticating Against Active Directory with Java on Linux
How to Negate a Method Reference Predicate
How to Replace Special Characters in a String