55 lines
1.7 KiB
C
Executable file
55 lines
1.7 KiB
C
Executable file
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* ft_addr_to_str.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2024/10/22 16:54:09 by thrieg #+# #+# */
|
|
/* Updated: 2025/02/16 19:02:50 by thrieg ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "libft.h"
|
|
#include <stdint.h>
|
|
|
|
void ft_convert_to_hexa(char c, char hex_buff[2])
|
|
{
|
|
hex_buff[1] = c & 0b00001111;
|
|
hex_buff[0] = (c >> 4) & 0b00001111;
|
|
if (hex_buff[0] <= 9)
|
|
hex_buff[0] += '0';
|
|
else
|
|
hex_buff[0] += 'a' - 10;
|
|
if (hex_buff[1] <= 9)
|
|
hex_buff[1] += '0';
|
|
else
|
|
hex_buff[1] += 'a' - 10;
|
|
}
|
|
|
|
//convert the address addr in hexadecimal and returns a standard c string.
|
|
//returns NULL if the allocation fails
|
|
char *ft_addr_to_strhex(void *addr)
|
|
{
|
|
char hex_buff[2];
|
|
int i;
|
|
int j;
|
|
uintptr_t address;
|
|
char *ret;
|
|
|
|
ret = malloc((sizeof(char) * sizeof(uintptr_t) * 2) + 1);
|
|
if (!ret)
|
|
return (NULL);
|
|
address = (uintptr_t)addr;
|
|
i = (sizeof(uintptr_t) * 8) - 8;
|
|
j = 0;
|
|
while (i >= 0)
|
|
{
|
|
ft_convert_to_hexa(((address >> i) & 0xFF), hex_buff);
|
|
ret[j++] = hex_buff[0];
|
|
ret[j++] = hex_buff[1];
|
|
i -= 8;
|
|
}
|
|
ret[j] = '\0';
|
|
return (ret);
|
|
}
|