hex.c 617 B

123456789101112131415161718192021222324252627282930
  1. #include <assert.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <ctype.h>
  5. #include <stdint.h>
  6. #define COLUMNS 16
  7. #define PADDING 6
  8. uint8_t buffer[COLUMNS];
  9. int main(void)
  10. {
  11. while (!feof(stdin)) {
  12. size_t n = fread(buffer, sizeof(buffer[0]), COLUMNS, stdin);
  13. for (size_t i = 0; i < n; ++i) {
  14. printf("%02X ", buffer[i]);
  15. if (!isprint(buffer[i])) buffer[i] = '.';
  16. }
  17. assert(n <= COLUMNS);
  18. printf("%*s", (int) (PADDING + (COLUMNS - n) * 3), "");
  19. fwrite(buffer, sizeof(buffer[0]), n, stdout);
  20. printf("\n");
  21. }
  22. return 0;
  23. }