Skip to content

Instantly share code, notes, and snippets.

@sasaki-shigeo
Created January 17, 2018 05:25
Show Gist options
  • Save sasaki-shigeo/c3470c14e30127b6ac9d4c795cd8a216 to your computer and use it in GitHub Desktop.
Save sasaki-shigeo/c3470c14e30127b6ac9d4c795cd8a216 to your computer and use it in GitHub Desktop.
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char *argv[]) {
FILE *fp;
if (argc < 2) {
fp = stdin;
}
else {
fp = fopen(argv[1], "r");
if (fp == NULL) {
fprintf(stderr, "cannot open: %s\n", argv[1]);
exit(1);
}
}
char buf[BUFSIZ];
size_t sz;
int seq = 0;
while ((sz = fread(buf, 1, BUFSIZ, fp)) != 0) {
for (int i = 0; i < sz; i++) {
if ((seq & 0x0f) == 0) {
printf("%04X : ", seq);
}
printf("%02X ", buf[i] & 0xff);
seq++;
if ((seq & 0x0f) == 0) {
putchar('\n');
}
}
}
putchar('\n');
fflush(stdout);
if (feof(fp)) {
return 0;
}
else {
return ferror(fp);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment