Skip to content

Instantly share code, notes, and snippets.

@Prince781
Created July 7, 2014 06:14
Show Gist options
  • Select an option

  • Save Prince781/e2c1352a4c5f096e90da to your computer and use it in GitHub Desktop.

Select an option

Save Prince781/e2c1352a4c5f096e90da to your computer and use it in GitHub Desktop.
Binary string to number converter
#include <stdio.h>
unsigned bintoint(char s[]) {
int i, l;
unsigned v = 0;
for (l=-1; s[l+1] != '\0'; ++l);
for (i=l; i>=0; --i)
v += (s[i]-'0')<<(l-i);
return v;
}
int main(int argc, char *argv[]) {
if (argc > 1) printf("%u\n", bintoint(argv[1]));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment