title: C - Encoding and decoding message using least significant bit
body: <p>My task is to decode and encode message using least significant bit. Message is encrypted in least significant bits. To decode one letter I must check eight consecutive bytes(eight numbers). I must use bit field in union for this exercise. Decoding in my program works perfectly but I have problem with encoding.
  Function encode doesn't encode anything. What is wrong with this function?</p>
  <pre><code>
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;stdint.h&gt;
  #include &lt;string.h&gt;
  
  union bit_set
  {
      signed char x;
      struct tab
      {
          unsigned char one: 1;
          unsigned char two: 1;
          unsigned char three: 1;
          unsigned char four: 1;
          unsigned char five: 1;
          unsigned char six: 1;
          unsigned char seven: 1;
          unsigned char eight: 1;
      } tab;
  };
  
  int decode(const char * array2, char *txt)
  {
      union bit_set bit_set;
      
      int i = 0;
      int j = 0;
      while(*(array2 + i) != '\0')
      {
          signed char number = 0;
          for(int k = 0; k &lt; 8; k++)
          {
              number = number &lt;&lt; 1;
              
              bit_set.x = *(array2 + i);
              number = number + bit_set.tab.one;
              i++;
          }
          *(txt + j) = (char)number;
         
          if(*(txt+j) == 0)
          {
              break;
          }
          j++;   
      }
      return 0;
  }
  
  int encode(const char *input, char *txt)
  {
      int i = 0;
      int j = 0;
      int len = strlen(input);
      
      while(j &lt;= len)
      {
          union bit_set bit_set;
          bit_set.x = (signed char)*(input + j);
          for(int k = 0; k &lt; 8; k++)
          {
              *(txt+i) &amp;= 0xFE;
              *(txt+i) += bit_set.tab.eight;
              *(txt + i) &lt;&lt;= 1;
              
          }
          i++;
          j++;
      }
      int h = 0;
      while(h &lt; 1000)
      {
          printf(&quot;%hhu &quot;, *(txt + h));
          h++;
      }
  
      return 0;
  }
  
  int main()
  {
      char msg[10000] = {0};
      char array[31] = &quot;p&quot;; //text for encode
      char array2[10000] = {100, 193, 113, 189, 184, 112, 148, 2, 244, 53, 97, 96, 100, 82, 96, 85, 64, 97, 97, 226, 56, 191, 217, 51, 76, 19, 155, 96, 236, 247, 10, 181, 
  82, 42, 188, 4, 78, 178, 210, 86, 90, 110, 94}; //word 'page' for decode
      
      encode(array, msg);
      
      //decode(array2, msg);
  
      //printf(&quot;%s&quot;, msg);
  
      return 0;
  }
  
  </code></pre>