Skip to content

Instantly share code, notes, and snippets.

@hugolu
Created January 1, 2017 04:28
Show Gist options
  • Save hugolu/334f068409cbc8365ce5b476c23aa892 to your computer and use it in GitHub Desktop.
Save hugolu/334f068409cbc8365ce5b476c23aa892 to your computer and use it in GitHub Desktop.
hexdump in c
#include <stdio.h>
#define MIN(A,B) ((A) < (B) ? (A) : (B))
void hexdump(void *buf, int size) {
unsigned char *ptr;
int i, j, len = 0;
for (i = 0; i < size; i += len) {
ptr = buf + i;
len = MIN(16, size - i);
printf("%04x | ", i);
for (j = 0; j < len; j++) {
printf("%02x ", ptr[j]);
}
printf("\n");
}
}
int main() {
unsigned char buf[256];
int i;
for (i = 0; i < 256; i++) buf[i] = (unsigned char)i;
hexdump (buf, 256);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment