/******************************************/ /* hexbin.c 1.00 */ /* by Adam M. Costello */ /* */ /* */ /* Translates a text file to a binary */ /* file according to the rules: */ /* */ /* Define a hex character as any one of: */ /* the digits 0 through 9 */ /* the letters A through F */ /* the letters a through f */ /* a space */ /* a tab */ /* **************************/ /* On each line, characters are ignored until a colon is reached. */ /* The colon is ignored, but subsequent characters are considered */ /* until a non-hex character is reached. This non-hex character */ /* is ignored, as are all remaining characters. Of the considered */ /* characters, all spaces and tabs are thrown out. If the number */ /* of remaining characters is odd, the last one is thrown out. */ /* The remaining characters are taken in pairs to represent bytes. */ /*******************************************************************/ /* This is ANSI C code. */ #include #define hexval(c) ((c) >= '0' && (c) <= '9' ? \ (c) - '0' : \ (c) >= 'A' && (c) <= 'F' ? \ (c) - 'A' + 10 : \ (c) >= 'a' && (c) <= 'f' ? \ (c) - 'a' + 10 : \ -1 ) int main(int argc, char **argv) { int c, ph, v[2]; for (;;) { do if ((c = getchar()) == EOF) return 0; while (c != ':'); ph = 0; for (;;) { do c = getchar(); while (c == ' ' || c == '\t'); if ((v[ph] = hexval(c)) < 0) break; if (ph) putchar(16 * v[0] + v[1]); ph = !ph; } while (c != '\n') { if (c == EOF) return 0; c = getchar(); } } }