im trying to convert string to binary in C. This function must be return a string(char *) like "010010101" etc. Also i want to print what returned. I cant be sure about this code
FUNCTION
char* stringToBinary(char* s)
{
if(s == NULL) return 0; /* no input string */
char *binary = malloc(sizeof(s)*8);
strcpy(binary,"");
char *ptr = s;
int i;
for(; *ptr != 0; ++ptr)
{
/* perform bitwise AND for every bit of the character */
for(i = 7; i >= 0; --i){
(*ptr & 1 << i) ? strcat(binary,"1") : strcat(binary,"0");
}
}
return binary;
}