In addition to the errors alk pointed out, you have four more problems.
1- This:
char * p = (char*)malloc(sizeof(char*)*242);
Should be this:
char * p = malloc(sizeof(char)*243); // Do NOT cast malloc
2- This:
sprintf(p,"%s%c", p, hex_digits[ ( rand() % 16 ) ]);
Should be this:
sprintf(p+i,"%c",hex_digits[ ( rand() % 16 ) ]);
3- You're missing this:
*(p+242) = '\0';
return p;
4- And finally, this line of code:
binaryMessage = string2bin(s);
Replaces the binaryMessage
pointer with the temp
pointer value.
The best advice I can offer? Get rid of all the globals. And you need to seed the random number generator with something unpredictable (current time, for example) or you'll get the same sequence of "random" numbers every time.