Convert a binary String to an Integer

Hello everyone,
I am trying to convert a binary String to an Integer and I wrote the following Code:

void setup() {
  Serial.begin(9600);
  Serial.print(binToDec("11111"));
}

void loop() {
}

int binToDec(String bin) {
  int l = bin.length();
  int dec = 0;

  for (int i = 0; i < l; i++) {
    if (bin.substring(l - i - 1, l - i) == "1") {
      dec += pow(2, i); 
    }
  }

  return dec;
}

Why isnt this working? If I put "11111" as input the result is 28. If I put "11" the result is 3 but when I put "100" the result is also 3. Why? What am I missing? I really dont see what is wrong, probably a stupid misstake.

And I also want to say that I want to use my code so please dont give me a completely new code. I know that it is possible to code it in a shorter way.
Thanks

You can solve it yourself by putting serial debug prints on all variables.

consider the following

int
binStr (
    char  *s )
{
    int  v      = 0;

    for (int i = 0; i < strlen (s); i++)  {
        v <<= 1;
        if ('1' == s [i])
            v += 1;
    }

    return v;
}

Ok thanks for the answers. I tried to find the misstake by debugging with Serial.print but it didnt help. I still couldnt find the misstake.
And as I already said I would like to fix my own code. At least tell me why it is wrong, even if the code is not the best.

badeyy:
Ok thanks for the answers. I tried to find the misstake by debugging with Serial.print but it didnt help. I still couldnt find the misstake.
And as I already said I would like to fix my own code. At least tell me why it is wrong, even if the code is not the best.

Well, I was kind of hoping you would post the results of that debug print here...

Did you print the value of 'pow(2, i)' to 8 digits? I think you will get a nasty surprise there.

No I did not but I asked a friend and he found the misstake and now I know what you mean :slight_smile:

if (bin.substring(l - i - 1, l - i) == "1")

this looks confusing. based on the description of substring(), i think both arguments should be the same.

and since you calculate the value based on the index, it doesn't matter if you work from left-to-right or the reverse, so why not use "i".

but i hope you see that if the string is just an ascii array, it's easy to access a specific character.

Bjarne Stroustrup in the 3rd edition of his book warned users to just use features of the language that are necessary and avoid using more sophisticated features

As my friend found out the Arduino doesnt have a chip for calculating the power normal. Instead the pow()-Function works like this: pow(x, y) = exp(x*ln(y)). That results in a float that is only nearly the right value. For example: 1000 wont be 8 excatly instead it would be like 7.999999 or something like this so when I convert the float to an int the result wont be 8, instead it will be 7.

Now you have to find a different way. One way to get integer powers of 2 is to... oh, wait, you said... "And I also want to say that I want to use my code so please dont give me a completely new code."

By the way, a "chip" (floating point arithmetic coprocessor) will not behave any better in that regard, it also produces approximations.

I wrote that because I thought that I made a logical mistake. But as we found out is wasnt a logical mistake. Now I see that I have to use other code.