42_ft_printf/ft_ptoa.c

38 lines
1.3 KiB
C
Raw Normal View History

2022-10-06 20:30:03 -04:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ptoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/06 17:41:04 by cchauvet #+# #+# */
2022-10-10 13:33:47 -04:00
/* Updated: 2022/10/10 15:54:00 by cchauvet ### ########.fr */
2022-10-06 20:30:03 -04:00
/* */
/* ************************************************************************** */
2022-10-10 13:33:47 -04:00
#include "printf.h"
2022-10-06 20:30:03 -04:00
char *ft_ptoa(void *ptr)
{
unsigned long address;
size_t i;
char *out;
char *s;
2022-10-10 13:33:47 -04:00
if (ptr == NULL)
return (ft_strdup("(nil)"));
2022-10-06 20:30:03 -04:00
address = (unsigned long) ptr;
2022-10-10 13:33:47 -04:00
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;
2022-10-06 20:30:03 -04:00
while (s[i] != 0)
{
out[i + 2] = s[i];
i++;
}
return (out);
}