title: Weird characters randomly appearing when converting int arr to string in C (with valid ASCII)
body: <p>This is a simple Caesar cipher implementation in C.</p>
  <p>I take a pin, make a key from it, take a message, shift the ASCII value w.r.t the key, and output the hex value of each character in hex.</p>
  <p>Upon using (as it appeared to me) <em>any key</em> , for the some (not all) messages results in the printing of an extra character upon decoding.</p>
  <ul>
  <li>Example Cases :</li>
  </ul>
  <p><strong>Weird Char appearing :</strong></p>
  <p>Message : &quot;<code>hexa !</code>&quot;</p>
  <p>Pin : <code>454545</code> &amp; Key : <code>23</code></p>
  <p>Ciphertext : (in hexa) &quot; <code>51 4e 61 4a 9 a fffffff3 4e -6f</code> &quot; (-6f is simply used to terminate input)</p>
  <p>Text given when decoding :</p>
  <pre><code>hexa !
  e
  </code></pre>
  <p>Other keys generate other weird chars , like ' <code>+</code> ', for example. The weird char always appears on the next line.</p>
  <hr />
  <p>The entire code is ~ 100 lines, so I wont paste it here, but it's available <a href="https://github.com/a-p-jo/C5/blob/master/C5.c" rel="nofollow noreferrer">on GitHub</a> .
  Don't use the windows .exe in this repo, it is of the older version, I'm trying to fix this issue before releasing this version.</p>
  <p>The code where the issue is likely appearing is the encrypt() and decrypt() functions :</p>
  <pre><code>void encrypt() {
       char msg[3001], ch;
       int i,key, en[3001], count = 0;
  
       printf(&quot;\n&quot;);
       key = pin();
       getchar();
       printf(&quot;\nType Message - \n\n&quot;);
       fgets(msg,sizeof(msg),stdin);
  
       for (i=0; msg[i] != '\0'; i++) {
           ch=msg[i];
           int d = (ch - key);
           en[i] = d;
           count++;
       }
  
       printf(&quot;\nEncrypted message -\n\n&quot;);
  
       for (i=0; i &lt;= count; i++) 
           printf(&quot;%x &quot;, en[i]);
  
       printf(&quot;-6f&quot;);
  }
  
  void decrypt() {
       char msg[3001], ch;
       int i,key, en[3001],d;
  
       printf(&quot;\n&quot;);
       key = pin();
       printf(&quot;\nEnter encrypted message -\n\n&quot;);
       getchar();
  
       for (i=0; i &lt;= 3001; i++) {
           scanf(&quot;%x&quot;,&amp;d);
  
           if (d == -111) {
              msg[i] = '\0';
              break;
           } else {
              ch = d + key;
              msg[i] = ch;
           }
       }
  
       printf(&quot;\nDecrypted message -\n\n&quot;);
       puts(msg);
  }
  </code></pre>