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