title: Converting from a Uint8Array to a string and back
body: <p>I'm having an issue converting from a particular Uint8Array to a string and back. I'm working in the browser and in Chrome which natively supports the TextEncoder/TextDecoder modules.</p>
  
  <p>If I start with a simple case, everything seems to work well:</p>
  
  <p><code>
  const uintArray = new TextEncoder().encode('silly face demons');
  // Uint8Array(17) [115, 105, 108, 108, 121, 32, 102, 97, 99, 101, 32, 100, 101, 109, 111, 110, 115] 
  new TextDecoder().decode(uintArray); // silly face demons
  </code></p>
  
  <p>But the following case is not giving me the results I expect. Without getting into too much of the details (it's cryptography related), let's start with the fact that I'm provided with the following Uint8Array:</p>
  
  <p><code>Uint8Array(24) [58, 226, 7, 102, 202, 238, 58, 234, 217, 17, 189, 208, 46, 34, 254, 4, 76, 249, 169, 101, 112, 102, 140, 208]</code></p>
  
  <p>and what I want to do is to convert that to a string and then later decrypt the string back to the original array, but I get this:</p>
  
  <p><code>
  const uintArray = new Uint8Array([58, 226, 7, 102, 202, 238, 58, 234, 217, 17, 189, 208, 46, 34, 254, 4, 76, 249, 169, 101, 112, 102, 140, 208]);
  new TextDecoder().decode(uint8Array); // :�f��:����."�L��epf��
  new TextEncoder().encode(':�f��:����."�L��epf��');
  </code></p>
  
  <p>...which results in:
  <code>Uint8Array(48) [58, 239, 191, 189, 7, 102, 239, 191, 189, 239, 191, 189, 58, 239, 191, 189, 239, 191, 189, 17, 239, 191, 189, 239, 191, 189, 46, 34, 239, 191, 189, 4, 76, 239, 191, 189, 239, 191, 189, 101, 112, 102, 239, 191, 189, 239, 191, 189]</code></p>
  
  <p>The array has doubled. Encoding is a bit out of my wheel house. Can anyone tell me why the array has doubled (I'm assuming it's an alternate representation of the original array...?). Also, and more importantly, is there a way I could get back to the original array (i.e. undouble the one I'm getting)?</p>