From 4b178c0feb4c415be36be0e4c0def8c447ed42af Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Fri, 22 Nov 2019 22:17:54 +0100 Subject: Added the most awesome c lib --- src/mlibc/stdlib/htoi.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/mlibc/stdlib/htoi.c (limited to 'src/mlibc/stdlib/htoi.c') diff --git a/src/mlibc/stdlib/htoi.c b/src/mlibc/stdlib/htoi.c new file mode 100644 index 0000000..73a583b --- /dev/null +++ b/src/mlibc/stdlib/htoi.c @@ -0,0 +1,22 @@ +#include +#include +#include + +int htoi(char *str) { + size_t s_str = strlen(str); + + size_t i = 0; + int ret = 0; + for (; i < s_str; i++) { + char c = str[i]; + int aux = 0; + if (c >= '0' && c <= '9') + aux = c - '0'; + else if (c >= 'A' && c <= 'F') + aux = (c - 'A') + 10; + + ret += aux * pow(16, (s_str - i) - 1); + } + + return ret; +} \ No newline at end of file -- cgit v1.2.3