diff options
author | Pixel <> | 2001-03-05 14:25:57 +0000 |
---|---|---|
committer | Pixel <> | 2001-03-05 14:25:57 +0000 |
commit | deb75e32768c82d2945427d558f73c64c4c2f72d (patch) | |
tree | 491f7a5a48cc7c70f2fa6373e5946956296cc6bf /lib/numbers.c | |
parent | ec68b01357f608f7800da530fc14f41d32cb2b45 (diff) |
Ajout de numbers
Diffstat (limited to 'lib/numbers.c')
-rw-r--r-- | lib/numbers.c | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/lib/numbers.c b/lib/numbers.c new file mode 100644 index 0000000..8334dd4 --- /dev/null +++ b/lib/numbers.c @@ -0,0 +1,58 @@ +#include "numbers.h" + +int char_to_number(char *st, int *valid) +{ + int whattype = 0, result = 0; + + *valid = 0; + + if (*st == '0') { + st++; + if (*st == 'x') { + whattype = 1; + st++; + } else if (*st) { + whattype = 2; + } else { + *valid = 1; + return 0; + } + } + + while (*st) { + switch (whattype) { + case 0: + if ((*st < '0') || (*st > '9')) { + return 0; + } + result *= 10; + result += *st - '0'; + break; + case 1: + if (((*st < '0') || (*st > '9')) && ((*st < 'A') || (*st > 'F')) + && ((*st < 'a') || (*st > 'f'))) { + return 0; + } + result *= 16; + if ((*st >= '0') && (*st <= '9')) { + result += *st - '0'; + } else if ((*st >= 'A') && (*st <= 'F')) { + result += *st - 'A' + 10; + } else { + result += *st - 'a' + 10; + } + break; + case 2: + if ((*st < '0') || (*st > '7')) { + return 0; + } + result *= 8; + result += *st - '0'; + break; + } + st++; + } + + *valid = 1; + return result; +} |