How to convert a Binary String to a base 10 integer in Java
You need to specify the radix. There's an overload of Integer#parseInt()
which allows you to.
int foo = Integer.parseInt("1001", 2);
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.
Converting a binary string to integer
char *binaryString[100];
// You are creating an array of pointers in this scenario, use char binaryString[100]
instead;
int value = (int)strtol(binaryString, NULL, 2);
// 101010111100110100110001001001000101
Is a 36 bit number, int
(in most implementations) is 32 bit. use long long
(64 bit in visual c++) as type and strtoll
as function instead.
printf("%i \n",value)
Must be printf("%lld \n", value)
.
In summary:
#include "stdio.h"
#include "stdlib.h" // required for strtoll
int main(void)
{
char str[100] = "101010111100110100110001001001000101";
long long val = 0;
val = strtoll(str, NULL, 2);
//output string as int
printf("%lld \n", val);
return 0;
}
How to convert Binary String to int array?
I know symptomsBit.split(" ") isn't correct. But do not know how to
further improve it.
Yes, you're almost there but must have made a few minor mistakes.
For one, that split()
isn't correct because you're trying to split it on " "
s but what you need is ""
s because it's a continuous string.
That will give you the array that you think you're getting. But you also don't print the elements right. You should index into the array to get the element to print. Like so...
int symptomsM = 867;
String symptomsBit = Integer.toBinaryString(symptomsM);
String[] symptomsBitArr = symptomsBit.split("");
System.out.println("symptomsBit: " + symptomsBit);
System.out.println("symptomsBitArray: " + symptomsBitArr);
int[] symptomsArray = new int[symptomsBitArr.length];
for (int i = 0; i < symptomsBitArr.length; i++) {
symptomsArray[i] = Integer.parseInt(symptomsBitArr[i]);
System.out.println("symptomsArray: " + symptomsArray[i]);
}
java: convert binary string to int
From http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Integer.html#toBinaryString(int) : the toBinaryString()
method converts its input into the binary representation of the "unsigned integer value is the argument plus 232 if the argument is negative".
From http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Integer.html#parseInt(java.lang.String,%20int) : the parseInt()
method throws NumberFormatException
if "The value represented by the string is not a value of type int
".
Note that both ~0
and ~1
are negative (-1 and -2 respectively), so will be converted to the binary representations of 232-1 and 232-2 respectively, neither of which can be represented in a value of type int
, so causing the NumberFormatException
that you are seeing.
Convert binary string into integer
Convert.ToInt32(String, Int32) lets you specify the base:
int output = Convert.ToInt32(input, 2);
Converting an int to a binary string representation in Java?
Integer.toBinaryString(int i)
how can i convert binary string to integer in android studio?
int num = Integer.parseInt("110", 2);
where 2
is your base (or radix). Here is a
link to the official documentation.
How to convert binary representation of number from string to integer number in JavaScript?
If you want to convert the array back to a string use join()
(MDN) and for converting a string to an integer use parseInt()
(MDN). The second argument of the later is an optional radix.
JavaScript will try to determine, what radix to use, but to be sure you should always add your radix manually. Citing from MDN:
If radix is undefined or 0, JavaScript assumes the following:
If the input string begins with "0x" or "0X", radix is 16 (hexadecimal).
If the input string begins with "0", radix is eight (octal). This feature is non-standard, and some implementations deliberately do not support it (instead using the radix 10). For this reason always specify a radix when using parseInt.
If the input string begins with any other value, the radix is 10 (decimal).
So in your case the following code should work:
var a = '01001011';
var b = parseInt( a.split('').reverse().join(''), 2 );
or just (if you would want to convert the starting string, without the reversal):
var b = parseInt( a, 2 );
Related Topics
Linq Where List Contains Any in List
How to Extract Text from Ms Office Documents in C#
What's the Difference Between the Webconfigurationmanager and the Configurationmanager
Random Number Between 2 Double Numbers
Convert String to Nullable Type (Int, Double, etc...)
How to Get the Index of an Item in a List in a Single Step
Performance of Static Methods VS Instance Methods
Can a Unit Test Project Load the Target Application's App.Config File
Httpwebrequest Is Extremely Slow!
How to Get the Time Difference Between Two Datetime Objects Using C#
Send Smtp Email Using System.Net.Mail via Exchange Online (Office 365)
How to Show File Copy Progress Using Fileinfo.Copyto() in .Net
Why Can't You Use the Keyword 'This' in a Static Method in .Net
Combobox- Selectionchanged Event Has Old Value, Not New Value