title: Javascript Convert String to Byte Array using ISO-8859-1 encoding
body: <p>Using Javascript, how can we convert a string to a byte array (byte representation using ISO-8859-1 encoding?</p>
  
  <p><strong>My Attempt:</strong></p>
  
  <pre><code>function string2byte(str) {
      let byteArray = []
      for (let j=0; j &lt; str.length; j++) {
          byteArray.push(str.charCodeAt(j));
      }
  
      return byteArray
  }
  
  let input = 'helloWorld';
  console.log(string2byte(input));
  </code></pre>
  
  <p>gives the output</p>
  
  <pre><code>[
      104, 101, 108, 108,
      111,  87, 111, 114,
      108, 100
  ]
  </code></pre>
  
  <p>Since Javascript strings uses UTF-16 representation, I am guessing the above code is wrong?</p>
  
  <p><em>(Not knowing Java)</em> I believe we need a Javascript version of Java's</p>
  
  <pre><code>import java.nio.charset.StandardCharsets;
  
  input.getBytes(StandardCharsets.ISO_8859_1);
  </code></pre>
  
  <p>Thank you for any help!!</p>
  
  <hr>
  
  <p><strong>Additional details:</strong> I'm actually trying to follow instructions for using an API:</p>
  
  <p>Input is a byte array composed of ordered fields using zero byte (0x00) as a separator. There is no separator before the first field or after the last field. Some fields are always empty in which case the separators immediately follow one another. If converting HTTP header values, and url parts from string to byte representation you should use ISO-8859-1 encoding. For request body you should use the raw bytes as they are sent to the server. For JSON messages the character encoding should always be UTF-8.</p>
  
  <p>Input structure is the following:</p>
  
  <ul>
  <li>API Key (eg: 4ebd366d-76f4-4400-a3b6-e51515d054d6)</li>
  <li>X-Time request header value (eg: 1543597115712)</li>
  </ul>