libprinthex provides a function to print a hex dump with colored sections.
Project State: Alive and maintained
Tested on Linux with Konsole and MacOS with Terminal
You should check the install.sh
script before executing.
The default installation path is /usr/local/{lib,include}
./build.sh
sudo ./install.sh
int printhex(void* buffer, int size, int bytesperline, ... );
This function prints the bytes of a buffer as a typical hex-view to the screen.
buffer points to the bytes that will be printed by printhex
.
This can be anything as long as there is read access to size bytes started at the address
buffer points to.
The size is the number of bytes that will be printed in total. It should be less or equal to the buffer buffer points to.
This parameter determines how many bytes will be printed in each line.
A good argument would be 16
.
The optional arguments are in alternating offset and color.
There can be as many pairs of offset and color as there are bytes in the buffer.
When the internal index to the values inside the buffer reached offset, the new text color gets read from the argument list.
Furthermore the next offset will be read. So the algorithm expects an infinite list of offsets and colors.
The last value has to be an offset that will never be reached (a good value is -1
).
The offset must be an integer between 0
and bytesperline - 1
.
Furthermore it must be greater than the previous offset given in the list or arguments.
It determines the offset from which a new color will be used to print the values of the buffer.
The color is the ANSI escape sequence for the color that will be used to print the values of the buffer after the offset related to that color is reached.
This function always returns 0
// Display Hello World, with World in green letters
char *helloworld = "Hello World";
printhex(helloworld, strlen(helloworld), 16,
6, "\e[1;32m", // Green World
-1); // (end of offset/color list)
printf("\n");
You will find a real example in the test.c file.