From: Nathael Pajani Date: Thu, 17 Sep 2015 18:46:41 +0000 (+0200) Subject: Add stdlib with strtoul() X-Git-Url: http://git.techno-innov.fr/?a=commitdiff_plain;h=55f57e29dd1d9fb2c2142536586784b5fc56db54;p=soft%2Flpc122x%2Fcore Add stdlib with strtoul() --- diff --git a/include/lib/stdlib.h b/include/lib/stdlib.h new file mode 100644 index 0000000..f1f1efa --- /dev/null +++ b/include/lib/stdlib.h @@ -0,0 +1,34 @@ +/**************************************************************************** + * lib/stdlib.h + * + * Copyright 2014 Nathael Pajani + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + *************************************************************************** */ + +#ifndef LIB_STDLIB_H +#define LIB_STDLIB_H + +#include + +/* Simple atoi implementation. + * Returns the value convertes from the given string. + * Does not check that the base is respected aside for the use of letters in + * number representation. + */ +uint32_t strtoul(uint8_t* str, uint8_t base); + +#endif /* LIB_STDLIB_H */ + diff --git a/lib/stdlib.c b/lib/stdlib.c new file mode 100644 index 0000000..be3b2b1 --- /dev/null +++ b/lib/stdlib.c @@ -0,0 +1,51 @@ +/**************************************************************************** + * lib/stdlib.c + * + * Copyright 2014 Nathael Pajani + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + *************************************************************************** */ + +#include + +/* Simple atoi implementation. + * Returns the value convertes from the given string. + * Does not check that the base is respected aside for the use of letters in + * number representation. + */ +uint32_t strtoul(uint8_t* str, uint8_t base) +{ + uint32_t val = 0; + while (*str != '\0') { + if (*str >= '0' && *str <= '9') { + val = (val * base) + ((*str) - '0'); + str++; + continue; + } + if (*str >= 'A' && *str <= 'F' && base > 10) { + val = (val * base) + ((*str) - 'A' + 10); + str++; + continue; + } + if (*str >= 'a' && *str <= 'f' && base > 10) { + val = (val * base) + ((*str) - 'a' + 10); + str++; + continue; + } + break; + } + return val; +} +