Hi everyone! I've been searching everywhere for the answer to what I think may be a basic question, but I really cannot find anything on how to convert a binary string to a byte/integer.
Essentially I want to do this:
String "0100 1010" --> converted to int 74... (preferably in as little steps as possible)
char s[] = "01001011";
int value = 0;
for (int i=0; i< strlen(s); i++) // for every character in the string strlen(s) returns the length of a char array
{
value *= 2; // double the result so far
if (s[i] == '1') value++; //add 1 if needed
}
Serial.println(value);
I would like to recommend checking the AVR C-Lib when in need. There a tons of standard C functions ready to be used. The above problem can be solved with a single function call.
Also I would recommend avoiding the Arduino String class as this often leads to "the dark side". The standard C string functions are more efficient and does not hide memory issues.