I have an exercise to do, I need to print the beginning of the address of my string, so to speak the [0]
position.
I use a function that will write it in the right format 00000hexahexa
, but I don't know how to send the address to the function, so it becomes a int.
Here is my code, the main function is at the bottom, size
is the number of characters in the string.
I try to send the address to the function printaddr
, but the compiler doesn't let me convert it to unsigned int
.
void convhexa(int nbr)
{
char *hexa;
hexa = "0123456789abcdef";
if (nbr > 15)
{
convhexa(nbr / 16);
convhexa(nbr % 16);
}
else
write(1, &hexa[nbr], 1);
}
void printaddr(unsigned int addr)
{
int count;
unsigned int nbr;
nbr = addr;
count = 0;
while (nbr > 0)
{
nbr /= 16;
count++;
}
while (count++ < 15)
write(1, "0", 1);
convhexa(addr);
write(1, ": ", 2);
}
void *ft_print_memory(void *addr, unsigned int size)
{
char *temp;
temp = addr;
while (size > 0)
{
printaddr(temp);
temp += 16;
size -= 16;
}
return (addr);
}