From 80fdb0714b75cb240d8b8068d718b915f1904110 Mon Sep 17 00:00:00 2001 From: Pixel <> Date: Thu, 29 Mar 2001 23:40:57 +0000 Subject: First. --- lib/numbers.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 lib/numbers.c (limited to 'lib/numbers.c') diff --git a/lib/numbers.c b/lib/numbers.c new file mode 100644 index 0000000..ba45f29 --- /dev/null +++ b/lib/numbers.c @@ -0,0 +1,59 @@ +#include "global.h" +#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; +} -- cgit v1.2.3