Home | History | Annotate | Line # | Download | only in puff
      1 #include "stdio.h"
      2 #include "stdlib.h"
      3 
      4 /**
      5  * Reads hexadecimal values from stdin and writes binary bytes to stdout.
      6  * Accepts hex values separated by spaces, newlines, commas, etc.
      7  * Handles both uppercase and lowercase hex digits.
      8  */
      9 int main(void) {
     10     char hexStr[3]; // Two hex digits + null terminator
     11     int ch;
     12 
     13     // Read characters until EOF
     14     while((ch = getchar()) != EOF) {
     15         hexStr[0] = (char)ch;
     16         hexStr[1] = (char)getchar();
     17         hexStr[2] = '\0'; // Null-terminate string
     18         char *endptr;
     19         unsigned char byte = (unsigned char)strtol(hexStr, &endptr, 16);
     20         fwrite(&byte, 1, 1, stdout);
     21         if((ch = getchar()) == EOF) // Read seaparating space
     22           break;
     23     }
     24 
     25     return 0;
     26 }
     27