05 Feb 2010
Binary String to Integer Conversion
Binary String to Integer Conversion in C
example by chrissie
// chrissie 10.2009
// Binary String to Integer conversion
#include <stdio.h>
//
// convert a binary string into dezimal values
//
int binary (char *c) {
int bval=0;
for (;*c;c++){
if (*c<'0'||*c>'1') return -1;
bval<<=1;
if (*c=='1') bval |=1;
}
return bval;
}
//
// main program
//
int main (void) {
int w;
char *s="1011001";
//char *s="1001";
w = binary(s);
printf("Bin: %s\n",s);
printf("Dez: %i\n",w);
}