54 lines
1.6 KiB
C
54 lines
1.6 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* ft_convert_base.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2024/06/17 13:54:50 by whaffman #+# #+# */
|
|
/* Updated: 2024/06/19 20:36:11 by whaffman ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
void ft_putnbr_base(int nbr, char *base, char *result);
|
|
int ft_atoi_base(char *str, char *base);
|
|
int ft_isbase(char *base);
|
|
|
|
char *ft_convert_base(char *nbr, char *base_from, char *base_to)
|
|
{
|
|
int i;
|
|
char *result;
|
|
|
|
if (ft_isbase(base_from) > 1 && ft_isbase(base_to) > 1)
|
|
{
|
|
result = (char *) malloc(33 * sizeof(char));
|
|
if (result)
|
|
{
|
|
*result = '\0';
|
|
i = ft_atoi_base(nbr, base_from);
|
|
ft_putnbr_base(i, base_to, result);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
result = (void *) 0;
|
|
}
|
|
return (result);
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
int main(void)
|
|
{
|
|
char *result;
|
|
|
|
result = (void *) 0;
|
|
result = ft_convert_base("-21474836489", "0123456789", "0123456789ABCDEF");
|
|
printf("%s", result);
|
|
free(result);
|
|
}
|
|
|
|
#endif
|