| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* ft_itoa_base.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: bchanot <bchanot@student.42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* 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);
- }
|