/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* ft_itoa_base.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: bchanot +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2018/10/12 03:26:09 by bchanot #+# #+# */ /* Updated: 2018/10/29 11:49:37 by bchanot ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" static int ft_ulen(t_uint64 value, t_uint8 base) { int i; i = 0; while (value / base) i++; return (i); } char *ft_utoa_base(t_uint64 value, t_uint8 base) { const char hex[16] = "0123456789abcdef"; char *s; int i; i = ft_ulen(value, base); s = ft_strnew(i); while (value / base) { s[i] = hex[value % base]; value /= base; i--; } return (s); } static int ft_len(t_int64 value, t_uint8 base) { int i; i = 0; while (value / base) i++; return (i); } char *ft_itoa_base(t_int64 value, t_uint8 base) { const char hex[16] = "0123456789abcdef"; char *s; int i; int neg; neg = value < 0 && base == 10 ? 1 : 0; value = value < 0 ? -value : value; i = ft_len(value, base) + neg; s = ft_strnew(i); while (value / base) { s[i] = hex[value % base]; value /= base; i--; } if (neg) s[i] = '-'; return (s); }