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);
Convert Binary String to Binary
Use this:
System.out.println(Integer.toBinaryString(Integer.parseInt("000",2))); // gives 0
System.out.println(Integer.toBinaryString(Integer.parseInt("010",2))); // gives 10
System.out.println(Integer.toBinaryString(Integer.parseInt("100",2))); // gives 100
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
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;
}
binary string to decimal number in c
binstr[cpts]
yields the ascii code of 0
or 1
(which is 0x30 or 0x31).
You need to use binstr[cpts] == '1'
to convert a ascii '1' to the number 1 and everything else to 0
(assuming that no other characters may occur). Another option would be binstr[cpts] - '0'
.
Btw, using the pow()
function is disregarded for such cases, better substitute pow(2,x)
by (1<<x)
.
for (cpts = 0; cpts <= 7; cpts++) {
x = 7 - cpts;
dec += ((binstr[cpts] == '1')*(1 << x));
}
There are many possibilities to make it look nicer, of course, the most obvious being (binstr[cpts] == '1') << x
.
Furthermore, mind that your code expects exactly 8 binary digits to calculate the correct result.
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.
Convert base-2 binary number string to int
You use the built-in int()
function, and pass it the base of the input number, i.e. 2
for a binary number:
>>> int('11111111', 2)
255
Here is documentation for Python 2, and for Python 3.
Related Topics
How to See Data from .Rdata File
Using Grep to Help Subset a Data Frame
Extract Elements Common in All Column Groups
Align Multiple Tables Side by Side
Data Table Merge Based on Date Ranges
Code Chunk Font Size in Rmarkdown with Knitr and Latex
Error: Could Not Find Function "%>%"
Ggplot2 Plot Without Axes, Legends, etc
Remove Grid, Background Color, and Top and Right Borders from Ggplot2
Network Chord Diagram Woes in R
How to Remove Columns from a Data.Frame
Differencebetween Gc() and Rm()
How to Save Data File into .Rdata
Spread with Duplicate Identifiers (Using Tidyverse and %>%)