--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_uitoa_base.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/09/05 09:46:11 by ljiriste #+# #+# */
+/* Updated: 2023/09/15 18:43:25 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include <stdlib.h> //malloc
+#include <stdint.h> //intmax_t
+#include "libft.h"
+
+static int size_needed(uintmax_t n, size_t base_len)
+{
+ int res;
+
+ if (n == 0)
+ return (2);
+ res = 1;
+ while (n != 0)
+ {
+ ++res;
+ n /= base_len;
+ }
+ return (res);
+}
+
+static int valid_base(const char *base)
+{
+ int i;
+ int j;
+
+ if (ft_strlen(base) < 2)
+ return (0);
+ i = 0;
+ while (base[i])
+ {
+ j = i + 1;
+ while (base[j])
+ if (base[i] == base[j++])
+ return (0);
+ ++i;
+ }
+ return (1);
+}
+
+char *ft_uitoa_base(uintmax_t n, const char *base)
+{
+ int size;
+ int base_len;
+ char *res;
+
+ if (!valid_base(base))
+ return (NULL);
+ base_len = ft_strlen(base);
+ size = size_needed(n, base_len);
+ res = malloc(size);
+ if (res == NULL)
+ return (res);
+ res[--size] = '\0';
+ if (n == 0)
+ res[0] = base[0];
+ while (n != 0)
+ {
+ res[--size] = base[n % base_len];
+ n /= base_len;
+ }
+ return (res);
+}
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/15 12:58:15 by ljiriste #+# #+# */
-/* Updated: 2023/09/05 12:06:26 by ljiriste ### ########.fr */
+/* Updated: 2023/09/15 18:43:23 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
char *ft_itoa(int n);
char *ft_itoa_base(intmax_t n, const char *base);
+char *ft_uitoa_base(uintmax_t n, const char *base);
char *ft_ctoa(char c);
char *ft_strmapi(const char *s, char (*f)(unsigned int, char));