/* * hex2bin - simple hex to bin filter * address@hidden - under GPL version 2 */ #include #include #include #include #include static int hex2bin (void) { char hex[3]; int d = 0; unsigned char c; int stdin_fd = fileno(stdin); int n_read; while ((n_read = read(stdin_fd, hex, sizeof(hex)-1)) > 0) { if (n_read == 1) { if (hex[0] != '\n') { fprintf (stderr, "input parse error, odd digits in hex file\n"); return (1); } return (1); } hex[2] = '\0'; sscanf (hex, "%x", &d); c = (unsigned char) d; printf ("%c", c); } return (0); } static int bin2hex (void) { int stdin_fd = fileno(stdin); int n_read; unsigned char c; #ifdef _WIN32 setmode (fileno(stdin), O_BINARY); setmode (fileno(stdout), O_BINARY); #endif while ((n_read = read(stdin_fd, &c, 1)) > 0) printf ("%.2x", c); return (0); } int main (int argc, char **argv) { if (argc >= 2 && strstr(argv[1], "-r")) return bin2hex(); return hex2bin(); }