Skip to content

Instantly share code, notes, and snippets.

@quanta-kt
Last active September 22, 2025 07:46
Show Gist options
  • Select an option

  • Save quanta-kt/cf694cc4bf2f88c0e0bf2f8b9033045e to your computer and use it in GitHub Desktop.

Select an option

Save quanta-kt/cf694cc4bf2f88c0e0bf2f8b9033045e to your computer and use it in GitHub Desktop.
Call xxd/hexdump from C
#ifndef HEXDUMP_H
#define HEXDUMP_H
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
void hexdump(char* data, size_t size) {
int fd[2];
pid_t pid;
char buf[128];
if (pipe(fd) == -1) {
perror("pipe");
exit(1);
}
pid = fork();
if (pid == -1) {
perror("fork");
exit(1);
}
if (pid == 0) {
close(fd[1]);
dup2(fd[0], STDIN_FILENO);
char *args[] = {"xxd", NULL};
execvp("xxd", args);
perror("exec");
exit(1);
} else {
close(fd[0]);
if (write(fd[1], data, size) == -1) {
perror("write");
}
close(fd[1]);
wait(NULL);
}
}
#endif // HEXDUMP_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment