#include <stdio.h>
#include <unistd.h>

void ft_putchar(char c)
{
    write(1, &c, 1);
}

void ft_putnbr(int nb)
{
    if (nb == -2147483647)
    {
        write(1, "-2147483647", 11);
    }
    else
    {
        if (nb < 0)
        {
            ft_putchar('-');
            nb = -nb;
        }
        if (nb >= 10)
        {
            ft_putnbr(nb / 10);
        }
        ft_putchar(nb % 10 + 48);
    }
}
/*
This is a recursive function ft_p_base that prints an integer nbr in a given
base represented by the string base. Here's a breakdown of the code:
    The function takes two arguments: nbr (an int) and base (a char*).
    The function calculates the length of the base string using a while
    loop. This is done to determine the number of unique digits in the base.
    The function checks if nbr is greater than or equal to the length of the
    base. If it is, the function recursively calls itself with nbr divided by
    the length of the base as the new nbr argument. This is done to handle the
    recursive conversion of the number to the desired base.
    If nbr is less than the length of the base, the function calls ft_putchar
    with base[nbr] as the argument. This prints the corresponding digit in the
    base for the current value of nbr.

The idea behind this function is to recursively divide the number by the base
and print the remainder as the next digit in the resulting string. The base string
is used as a lookup table to get the corresponding digit for each remainder.

For example, if you call ft_p_base(12, "0123456789ABCDEF"), the function will print
the decimal number 12 in hexadecimal format, which is "C".

Note that this function assumes that the base string is a valid base representation
(e.g., "0123456789" for decimal, "0123456789ABCDEF" for hexadecimal, etc.). 
Also, it does not handle errors or invalid inputs.
*/
void ft_p_base(int nbr, char *base)
{
	int	length;
	
	length = 0;
	while (base[length])
	{
		length++;
	}
	if (nbr >= length)
	{
		ft_p_base(nbr / length, base);
	}
	else
	{
		ft_putchar(base[nbr]);
	}
} 

int main() {
    ft_putnbr(42);
    ft_putchar('\n');
//    return 0;

    ft_p_base(12, "0123456789ABCDEF");

    ft_putchar('\n');

    return 0;

}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: