summaryrefslogtreecommitdiff
path: root/v4.0/src/CMD/FC/NTOI.C
blob: 267fce79ccfcf0f326e19017eac8b9311823eaf2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/* convert an arbitrary based number to an integer */

#include <ctype.h>
#include "tools.h"

/* p points to characters, return -1 if no good characters found
 * and base is 2 <= base <= 16
 */
int ntoi (p, base)
char *p;
int base;
{
    register int i, c;
    flagType fFound;

    if (base < 2 || base > 16)
	return -1;
    i = 0;
    fFound = FALSE;
    while (c = *p++) {
	c = tolower (c);
	if (!isxdigit (c))
	    break;
	if (c <= '9')
	    c -= '0';
	else
	    c -= 'a'-10;
	if (c >= base)
	    break;
	i = i * base + c;
	fFound = TRUE;
	}
    if (fFound)
	return i;
    else
	return -1;
}