aboutsummaryrefslogtreecommitdiff
path: root/src/kernel/lib/stdlib/htoi.c
blob: f2d4702fee08db606bcb1ba9c99ab4161beef193 (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
#include <kernel/lib/math.h>
#include <stddef.h>
#include <kernel/lib/string.h>

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;
}