#include <stdlib.h>
int ft_strlen(char *str);
int wordc(long nbr, char *base_to);
void fill_itoa(int *i, char *itoa, long nbr, char base_to);
char *ft_itoa_base(long nbr, char *base_to);
int check_base(char *base) //verif si la base est valide
{
int i;
int k;
i = 0;
if (base[i] == '\0' || base[i + 1] == '\0')
return (0);
while (base[i])
{
k = i + 0;
while (base[k])
{
k++;
if (base[i] == base[k] || base[i] == '+' || base[i] == '-'
|| base[i] == ' ' || (base[i] >= 9 && base[i] <= 13))
return (0);
}
i++;
}
return (1);
}
int is_index(char c, char *base)
// check si le char correspond a la base, retourne l index
{
int i;
i = 0;
while (base[i])
{
if (c == base[i])
return (i);
i++;
}
return (0);
}
int is_in_base(char c, char *base)
// check si le char est ds la base, retourne 1 si oui
{
int i;
i = 0;
while (base[i])
{
if (c == base[i])
return (1);
i++;
}
return (0);
}
int ft_atoi_base(char *str, char *base)
// juste un atoi selon la base strlenned
{
int i;
int sign;
long n;
i = 0;
n = 0;
sign = 0;
while ((str[i] >= 9 && str[i] <= 13) || str[i] == 32)
i++;
while (str[i] == '-' || str[i] == '+')
{
if (str[i] == '-')
sign++;
i++;
}
while (is_in_base(str[i], base) == 1)
{
n = (n * ft_strlen(base)) + is_index(str[i], base);
i++;
}
if (sign % 2 == 1)
return (n * -1);
return (n);
}
char *ft_convert_base(char *nbr, char *base_from, char *base_to)
// first = res de 1ere conv et resulyat de la 2e conv
{
long first;
char *resultat;
if (check_base(base_from) == 0)
return (NULL);
first = ft_atoi_base(nbr, base_from);
resultat = ft_itoa_base(first, base_to);
return (resultat);
}
#include <stdio.h>
int main(int ac, char **av)
{
(void)ac;
//char nbr[] = "123";
//char base_from[] = "0123456789";
//char base_to[] = "01";
printf("%s\n", ft_convert_base(av[1], av[2], av[3]));
}
To embed this project on your website, copy the following code and paste it into your website's HTML: