/* * hex2bin -- convert all hex strings in the form 0x12 into binary * hex2bin infile outfile * (c) 2005 Bernd 'Aard' Wachter, * http://bwachter.lart.info * You may use and redistribute this progrem under the terms and * conditions of the GNU GPL V2 */ #include #include #include #include #include #include #ifdef __WIN32__ #include #else #include #endif #ifdef __dietlibc__ #include #else static int __write1(const char *s){ write(1, s, strlen(s)); return 0; } static int __write2(const char *s){ write(2, s, strlen(s)); return 0; } #endif int main(int argc, char **argv){ int fd_in, fd_out, i=1; char buf[5]; char bytebuf[2]; long byte; if (argc!=3){ __write2("Usage: hex2bin infile outfile\n"); exit(-1); } if ((fd_in=open(argv[1], O_RDONLY))==-1){ __write2("Unable to open in-file\n"); exit(-1); } if ((fd_out=open(argv[2], O_RDWR | O_CREAT | O_TRUNC, S_IWRITE | S_IREAD))==-1){ __write2("Unable to open out-file\n"); exit(-1); } while (i>0){ if ((i=read(fd_in, buf, 1))==-1) goto bye; if (buf[0]!='0') continue; if ((i=read(fd_in, buf+1, 1))==-1) goto bye; if (buf[1]!='x') continue; if ((i=read(fd_in, buf+2, 2))!=2) goto bye; else { byte=strtol(buf, NULL, 16); sprintf(bytebuf, "%c", byte); write(fd_out, bytebuf, 1); } } bye: close(fd_in); close(fd_out); return 0; }