narnaud
3 years ago
7 changed files with 161 additions and 37 deletions
@ -0,0 +1,35 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* ft_itox.c :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2021/12/21 09:51:26 by narnaud #+# #+# */ |
||||
|
/* Updated: 2021/12/21 09:56:39 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "libft.h" |
||||
|
|
||||
|
char *ft_itox(unsigned long int n, const char *base) |
||||
|
{ |
||||
|
char *ret; |
||||
|
size_t i; |
||||
|
unsigned int neg; |
||||
|
size_t size; |
||||
|
|
||||
|
size = ft_longbaselen(n, 16); |
||||
|
if (n == 0) |
||||
|
return (ft_strdup("0")); |
||||
|
i = 0; |
||||
|
neg = 0; |
||||
|
ret = ft_calloc(size + 1, sizeof(char)); |
||||
|
while (n) |
||||
|
{ |
||||
|
ret[i++] = base[n % 16]; |
||||
|
n = n / 16; |
||||
|
} |
||||
|
ft_strrev(&ret, neg); |
||||
|
return (ret); |
||||
|
} |
@ -0,0 +1,50 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* ft_nbrlen.c :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2021/12/21 09:42:43 by narnaud #+# #+# */ |
||||
|
/* Updated: 2021/12/21 09:53:16 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "libft.h" |
||||
|
|
||||
|
size_t ft_ilen(int nbr) |
||||
|
{ |
||||
|
size_t i; |
||||
|
|
||||
|
i = 0; |
||||
|
if (nbr == 0) |
||||
|
return (1); |
||||
|
else if (nbr < 0) |
||||
|
i++; |
||||
|
while (nbr && ++i) |
||||
|
nbr = nbr / 10; |
||||
|
return (i); |
||||
|
} |
||||
|
|
||||
|
size_t ft_ulen(unsigned int nbr) |
||||
|
{ |
||||
|
size_t i; |
||||
|
|
||||
|
i = 0; |
||||
|
while (nbr && ++i) |
||||
|
nbr = nbr / 10; |
||||
|
return (i); |
||||
|
} |
||||
|
|
||||
|
size_t ft_longbaselen(long nbr, size_t base) |
||||
|
{ |
||||
|
size_t i; |
||||
|
|
||||
|
if (nbr <= 0) |
||||
|
i = 1; |
||||
|
else |
||||
|
i = 0; |
||||
|
while (nbr && ++i) |
||||
|
nbr = nbr / base; |
||||
|
return (i); |
||||
|
} |
@ -0,0 +1,32 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* ft_strrev.c :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2021/10/27 09:36:21 by narnaud #+# #+# */ |
||||
|
/* Updated: 2021/10/27 09:52:46 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "libft.h" |
||||
|
|
||||
|
void ft_strrev(char **str, unsigned int neg) |
||||
|
{ |
||||
|
char ch; |
||||
|
size_t size; |
||||
|
size_t i; |
||||
|
|
||||
|
size = ft_strlen(*str); |
||||
|
if (neg) |
||||
|
(*str)[size++] = '-'; |
||||
|
i = 0; |
||||
|
while (i < size / 2) |
||||
|
{ |
||||
|
ch = (*str)[i]; |
||||
|
(*str)[i] = (*str)[size - i - 1]; |
||||
|
(*str)[size - i - 1] = ch; |
||||
|
i++; |
||||
|
} |
||||
|
} |
@ -0,0 +1,33 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* utoa.c :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2021/12/21 09:48:38 by narnaud #+# #+# */ |
||||
|
/* Updated: 2021/12/21 09:49:12 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "libft.h" |
||||
|
|
||||
|
char *ft_utoa(unsigned int n) |
||||
|
{ |
||||
|
char *ret; |
||||
|
unsigned int i; |
||||
|
|
||||
|
if (n == 4294967295) |
||||
|
return (ft_strdup("4294967295")); |
||||
|
if (n == 0) |
||||
|
return (ft_strdup("0")); |
||||
|
i = 0; |
||||
|
ret = ft_calloc(ft_ulen(n) + 1, sizeof(char)); |
||||
|
while (n) |
||||
|
{ |
||||
|
ret[i++] = (n % 10) + '0'; |
||||
|
n = n / 10; |
||||
|
} |
||||
|
ft_strrev(&ret, 0); |
||||
|
return (ret); |
||||
|
} |
Loading…
Reference in new issue