#include <unistd.h>
void write_hex(long valeur, int largeur) {
char hex_digits[] = "0123456789abcdef";
char buffer[largeur];
for (int i = largeur - 1; i >= 0; --i) {
buffer[i] = hex_digits[valeur % 16];
valeur /= 16;
}
write(1, buffer, largeur);
}
void write_byte_as_hex(char byte) {
char hex_digits[] = "0123456789abcdef";
char paire_hex[2] = { hex_digits[byte / 16], hex_digits[byte % 16] };
write(1, paire_hex, 2);
}
void write_char(char caractere) {
if (caractere >= 32 && caractere <= 126) {
write(1, &caractere, 1);
} else {
char point = '.';
write(1, &point, 1);
}
}
void ft_print_memory(void *addr, unsigned int size) {
char *ptr = addr;
long addr_debut = (long)addr;
for (size_t i = 0; i < size; i += 16) {
write_hex(addr_debut + i, 16);
write(1, ": ", 2);
for (size_t j = 0; j < 16; ++j) {
if (i + j < size) {
write_byte_as_hex(ptr[i + j]);
} else {
write(1, " ", 2);
}
if (j % 2 == 1) {
write(1, " ", 1);
}
}
for (size_t j = 0; j < 16; ++j) {
if (i + j < size) {
write_char(ptr[i + j]);
} else {
char space = ' ';
write(1, &space, 1);
}
}
write(1, "\n", 1);
}
}
int main() {
char *mot = "Bonjour les aminches\t\n\tc est fou\ttout\tce qu on peut faire avec\t\n\tprint_memory\n\n\n\tlol.lol\n \0";
ft_print_memory(mot,92);
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: