plainhex/plainhex.c

39 lines
643 B
C

#include <assert.h>
#include <err.h>
#include <stdio.h>
char nybble(int n) {
assert(0 <= n && n <= 15);
return "0123456789abcdef"[n];
}
int main(void) {
for (;;) {
int byte = getchar();
if (byte == EOF) {
if (feof(stdin)) {
break;
} else {
err(1, "Error reading byte");
}
}
if (putchar(nybble(byte >> 4)) == EOF) {
err(1, "Error writing high nybble");
}
if (putchar(nybble(byte & 0xf)) == EOF) {
err(1, "Error writing low nybble");
}
}
if (putchar('\n') == EOF) {
err(1, "Error writing terminating newline");
}
if (fflush(stdout) == EOF) {
err(1, "Error flushing stdout");
}
return 0;
}