home *** CD-ROM | disk | FTP | other *** search
/ Media Share 13 / mediashare_13.zip / mediashare_13 / ZIPPED / PROGRAM / SNIP9404.ZIP / BSTR_I.C < prev    next >
C/C++ Source or Header  |  1994-04-03  |  694b  |  41 lines

  1. /*
  2. **  Make an ascii binary string into an integer.
  3. **
  4. **  Public domain by Bob Stout
  5. */
  6.  
  7. #include <string.h>
  8.  
  9. unsigned int bstr_i(char *cptr)
  10. {
  11.       unsigned int i, j = 0;
  12.  
  13.       while (cptr && *cptr && strchr("01", *cptr))
  14.       {
  15.             i = *cptr++ - '0';
  16.             j <<= 1;
  17.             j |= (i & 0x01);
  18.       }
  19.       return(j);
  20. }
  21.  
  22. #ifdef TEST
  23.  
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26.  
  27. int main(int argc, char *argv[])
  28. {
  29.       char *arg;
  30.       unsigned int x;
  31.  
  32.       while (--argc)
  33.       {
  34.             x = bstr_i(arg = *++argv);
  35.             printf("Binary %s = %d = %04Xh\n", arg, x, x);
  36.       }
  37.       return EXIT_SUCCESS;
  38. }
  39.  
  40. #endif /* TEST */
  41.