42_ft_printf/ft_ptoa.c
Camille Chauvet af946dbf97 v1
2022-10-10 19:33:47 +02:00

38 lines
1.3 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ptoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/06 17:41:04 by cchauvet #+# #+# */
/* Updated: 2022/10/10 15:54:00 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "printf.h"
char *ft_ptoa(void *ptr)
{
unsigned long address;
size_t i;
char *out;
char *s;
if (ptr == NULL)
return (ft_strdup("(nil)"));
address = (unsigned long) ptr;
s = ft_ultoabase(address, "0123456789abcdef");
out = malloc((ft_strlen(s) + 2) * sizeof(char *));
out[ft_strlen(s) + 2] = 0;
out[0] = '0';
out[1] = 'x';
i = 0;
while (s[i] != 0)
{
out[i + 2] = s[i];
i++;
}
return (out);
}