Created
June 24, 2011 18:13
-
-
Save endeav0r/1045343 to your computer and use it in GitHub Desktop.
display file in hex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
int main (int argc, char * argv[]) { | |
int filesize; | |
int i; | |
unsigned char byte; | |
char string[41]; | |
FILE * fh; | |
if (argc != 2) { | |
fprintf(stderr, "%s <filename>\n", argv[0]); | |
exit(0); | |
} | |
fh = fopen(argv[1], "rb"); | |
fseek(fh, 0, SEEK_END); | |
filesize = (int) ftell(fh); | |
fseek(fh, 0, SEEK_SET); | |
i = 0; | |
while (fread(&byte, 1, 1, fh)) { | |
if (i % 32 == 0) | |
printf("[%08x] ", i); | |
printf("%02x", byte); | |
if ((byte >= 0x20) && (byte < 0x7f)) | |
string[i%32] = byte; | |
else | |
string[i%32] = '.'; | |
string[(i%32) + 1] = 0; | |
i++; | |
if (i % 32 == 0) | |
printf(" %s\n", string); | |
else if (i % 4 == 0) | |
printf(" "); | |
} | |
if (i > 0) { | |
for (; i % 32 != 0; i++) { | |
if (i % 4 == 0) | |
printf(" "); | |
else | |
printf(" "); | |
} | |
printf(" %s\n", string); | |
} | |
else | |
printf("\n"); | |
fclose(fh); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment