home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / BSTR_I.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  739b  |  43 lines

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