Christopher Coale <chris95...@...> wrote:
> ...this converts a binary string("1001011101")
> to a decimal value(605).
> 
> long Bin2Dec(const char *bin)
> {
>     long p = 0;
>     long r = 0;
>     long len = strlen(bin) - 1;
>     for (long i = len; i >= 0; i--,p++)
>     {
>         if (bin[i] == '1')
>             r += powf((float)2, (float)p);
>     }
>     return r;
> }

  #include <stdio.h>
  #include <stdlib.h>
  
  int main(void)
  {
    const char bin[] = "1001011101";
    printf("%s: %ld\n", bin, strtol(bin, 0, 2));
    return 0;
  }

-- 
Peter

Reply via email to