Itoa custom
char *ft_itoa(int n) {
char buf[sizeof(int)*CHAR_BIT/3 + 3];
char *s;
unsigned int v;
v = n;
if (n < 0) {
v = -v;
}
s = buf + sizeof(buf);
*--s = '\\0';
while (v >= 10) {
*--s = '0' + v % 10;
v /= 10;
}
*--s = '0' + v;
if (n < 0)
*--s = '-';
return strdup(s);
}
Atoi custom
#include <malloc.h>
#include <string.h>
#include <limits.h>
int ft_atoi(const char* str) {
int i = 0, sign = 1, base = 0;
while(str[i] == 0x20)
i++;
if(str[i] == 0x2D || str[i] == 0x2B) {
sign = 1 - 2 * (str[i++] == 0x2D);
}
while (str[i] >= '0' && str[i] <= '9')
{
// handling overflow test case
if (base > INT_MAX / 10
|| (base == INT_MAX / 10
&& str[i] - '0' > 7))
{
if (sign == 1)
return INT_MAX;
else
return INT_MIN;
}
base = 10 * base + (str[i++] - '0');
}
return base * sign;
}