void hextobin(unsigned char *ctext, unsigned char *btext) { /* define buffers */ unsigned char *ibuff; /* input buffer */ unsigned char *obuff; /* output buffer */ unsigned char *itbuff; /* temp buffer pointer, so we don't destroy ibuff */ unsigned char *otbuff; /* temp buffer pointer, so we don't destroy obuff */ int i = 0; unsigned char *hexstring; /* temp string used for strtol */ int x = 0; /* allocate buffer memory and zero out */ itbuff = ibuff = calloc(256, sizeof(char)); /* set all pointers to start memory */ otbuff = obuff = calloc(128, sizeof(char)); /* set all pointers to start memory */ hexstring = calloc(4, sizeof(char)); memset(obuff, 0, sizeof(obuff)); strcpy(ibuff, ctext); strncpy(hexstring, "0x", 2); /* setup for hex conversion */ /* convert ibuff to obuff, using itbuff and otbuff */ for (i = 0 ; i < 128 ; i++) { hexstring[2] = itbuff[0]; /* grab first char */ hexstring[3] = itbuff[1]; /* grab second char */ printf("obuff %s\n", obuff); sprintf(otbuff, "%c", strtol(hexstring, NULL, 16)); /* convert to long, then put it in obuff as char */ (itbuff) += 2; /* walk input buffer, 2 chars at a time */ (otbuff)++; /* walk output buffer, 1 char at a time */ } /* copy the results */ strcpy(btext, obuff); printf("btext %s\n", btext); /* make sure we do proper memory management! */ free(obuff); free(ibuff); free(hexstring); }