aboutsummaryrefslogtreecommitdiff
path: root/src/kernel/lib/stdlib/htoi.c
blob: dcc4b63dd8b120c391e3fdf0fd6b2a7df2e8222f (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, (int)((s_str - i) - 1));
	}

	return ret;
}