diff options
| author | 2024-04-25 21:24:10 +0100 | |
|---|---|---|
| committer | 2024-04-25 22:32:27 +0000 | |
| commit | 2d04cacc5322951f187bb17e017c12920ac8ebe2 (patch) | |
| tree | 80ee017efa878dfd5344b44249e6a241f2a7f6e2 /v4.0/src/CMD/FC/NTOI.C | |
| parent | Merge pull request #430 from jpbaltazar/typoptbr (diff) | |
| download | ms-dos-main.tar.gz ms-dos-main.tar.xz ms-dos-main.zip | |
Diffstat (limited to 'v4.0/src/CMD/FC/NTOI.C')
| -rw-r--r-- | v4.0/src/CMD/FC/NTOI.C | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/v4.0/src/CMD/FC/NTOI.C b/v4.0/src/CMD/FC/NTOI.C new file mode 100644 index 0000000..267fce7 --- /dev/null +++ b/v4.0/src/CMD/FC/NTOI.C | |||
| @@ -0,0 +1,37 @@ | |||
| 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 | } | ||