Initial commit: add very simple hexdump

This commit is contained in:
g1n 2021-08-22 18:27:46 +03:00
commit d5fb5de386
5 changed files with 64 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*~

3
README.org Normal file
View File

@ -0,0 +1,3 @@
#+TITLE: hexutils - hexdump, xxd
Simple tools to work with binary files and display them in ascii, hex or binary and make hexdumps

11
src/Makefile Normal file
View File

@ -0,0 +1,11 @@
CC = gcc
CFLAGS= -Wall -Wextra
LFLAGS=
all: hexdump
main: $(OBJFILES)
$(CC) hexdump.c -o hexdump
test: main
./hexdump

49
src/hexdump.c Normal file
View File

@ -0,0 +1,49 @@
#include <stdio.h>
#include <string.h>
int dumpgroup = 0; // To know where newline should be
int hexnum = 1; // To put space after every second hex number
int chardump(char *data, int end_of_line /* to display only 16 chars */) {
for (int i=0; i < end_of_line; i++) {
if (data[i] == "\0")
printf(".");
else
printf("%c", data[i]);
}
printf("\n");
return 0;
}
int hexdump(char *data) {
//printf("%s\n", data); // FIXME
for (int i=0; i < strlen(data); i++) {
hexnum++;
if (data[i] == '\0')
printf("00");
else
printf("%02x", data[i]);
if (hexnum%2) {
printf(" ");
dumpgroup++;
}
if (dumpgroup == 8) {
dumpgroup = 0;
printf(" ");
chardump(data, i);
//printf("\n");
}
}
//printf("\n"); // FIXME
}
int main() {
FILE *infile = fopen("../tests/test.bin", "r");//fopen("test.bin", "r");
char data[50];
while (fgets(data, 50, infile) != NULL) {
hexdump(data);
}
printf("\n"); // FIXME
fclose(infile);
}

BIN
tests/test.bin Normal file

Binary file not shown.