]> wirehaze git hosting - MS-DOS.git/blob - v4.0/src/CMD/FC/NTOI.C

wirehaze git hosting

MZ is back!
[MS-DOS.git] / v4.0 / src / CMD / FC / NTOI.C
1 /* convert an arbitrary based number to an integer */
2
3 #include <ctype.h>
4 #include "tools.h"
5
6 /* p points to characters, return -1 if no good characters found
7 * and base is 2 <= base <= 16
8 */
9 int ntoi (p, base)
10 char *p;
11 int base;
12 {
13 register int i, c;
14 flagType fFound;
15
16 if (base < 2 || base > 16)
17 return -1;
18 i = 0;
19 fFound = FALSE;
20 while (c = *p++) {
21 c = tolower (c);
22 if (!isxdigit (c))
23 break;
24 if (c <= '9')
25 c -= '0';
26 else
27 c -= 'a'-10;
28 if (c >= base)
29 break;
30 i = i * base + c;
31 fFound = TRUE;
32 }
33 if (fFound)
34 return i;
35 else
36 return -1;
37 }