[ create a new paste ] login | about

Link: http://codepad.org/nKq1eflh    [ raw code | output | fork ]

C++, pasted on Jul 3:
#include <cstdio>
 
bool bin2dec (const char* bin, unsigned& dec) {
  if (!bin) return false;
  unsigned bin_pow = 1;
  char sym; 
  dec = 0;
  for ( ; (sym = *bin) != '\0' ; ++bin) {
    bin_pow *= 2;
    switch (sym) {
      case '0': break;
      case '1': dec += bin_pow; break;
      default:  return false;
    }
  }
  return true;
}
 
int main() {
  unsigned res = 0;
  if (bin2dec("11010", res)) {
     printf ("%d\n", res);
  }
}


Output:
1
22


Create a new paste based on this one


Comments: