str2long.c 668 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /* $Id$ */
  2. /*
  3. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  4. * See the copyright notice in the ACK home directory, in the file "Copyright".
  5. */
  6. /* str2long()
  7. */
  8. #include "ack_string.h"
  9. value(c, b)
  10. char c;
  11. int b;
  12. {
  13. register int ch;
  14. ch = c - '0';
  15. if ((unsigned) ch <= 9) return ch;
  16. ch = c - 'A';
  17. if ((unsigned) ch <= 5) return ch + 10;
  18. ch = c - 'a';
  19. if ((unsigned) ch <= 5) return ch + 10;
  20. return b;
  21. }
  22. long
  23. str2long(str, base)
  24. register char *str;
  25. int base;
  26. {
  27. int minus = 0, d;
  28. long l = 0;
  29. if (*str == '-') {
  30. minus++;
  31. str++;
  32. }
  33. while ((d = value(*str++, base)) < base)
  34. l = base * l + d;
  35. return minus ? -l : l;
  36. }