Version Final
This commit is contained in:
84
ft_aux_pf.c
Normal file
84
ft_aux_pf.c
Normal file
@@ -0,0 +1,84 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_aux_pf.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: htamayo- <htamayo-@student.42malaga.c +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/12/03 19:27:55 by htamayo- #+# #+# */
|
||||
/* Updated: 2025/12/03 19:57:43 by htamayo- ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_printf.h"
|
||||
|
||||
size_t ft_strlen_pf(const char *str)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
if (!str)
|
||||
return (0);
|
||||
i = 0;
|
||||
while (str[i])
|
||||
i++;
|
||||
return (i);
|
||||
}
|
||||
|
||||
void ft_bzero_pf(void *mem, size_t len)
|
||||
{
|
||||
unsigned char *temp;
|
||||
|
||||
temp = (unsigned char *)mem;
|
||||
while (len > 0)
|
||||
{
|
||||
*temp = 0;
|
||||
temp ++;
|
||||
len--;
|
||||
}
|
||||
}
|
||||
|
||||
void *ft_calloc_pf(size_t len, size_t size)
|
||||
{
|
||||
void *dest;
|
||||
|
||||
dest = malloc(len * size);
|
||||
if (dest == NULL)
|
||||
return (NULL);
|
||||
ft_bzero_pf(dest, len * size);
|
||||
return (dest);
|
||||
}
|
||||
|
||||
static size_t ft_len(unsigned long long n, char *base)
|
||||
{
|
||||
size_t len;
|
||||
unsigned long long base_len;
|
||||
|
||||
len = 1;
|
||||
base_len = ft_strlen_pf(base);
|
||||
while (n >= base_len)
|
||||
{
|
||||
n /= base_len;
|
||||
len++;
|
||||
}
|
||||
return (len);
|
||||
}
|
||||
|
||||
char *ft_aux_pf(unsigned long long n, char *base)
|
||||
{
|
||||
char *str;
|
||||
int num_len;
|
||||
int base_len;
|
||||
|
||||
num_len = ft_len(n, base);
|
||||
base_len = ft_strlen_pf(base);
|
||||
str = ft_calloc_pf((num_len + 1), sizeof(char));
|
||||
if (!str)
|
||||
return (NULL);
|
||||
while (num_len)
|
||||
{
|
||||
num_len--;
|
||||
str[num_len] = base[n % base_len];
|
||||
n /= base_len;
|
||||
}
|
||||
return (str);
|
||||
}
|
||||
Reference in New Issue
Block a user