title: JS vs Python byte array encoding
body: <p>I am trying to convert a byte array into a string and then send it over a socket to a remote server. I have successfully prototyped the code in <strong>Python</strong> and am trying to migrate it to <strong>Javascript</strong>.</p>
  
  <p>For some reason, there is a discrepancy in the last character between the two languages.</p>
  
  <p><strong>Python Code</strong></p>
  
  <pre><code>def make_checksum(data):
      num = 0x00
      for num2 in data:
          num = (num + num2) &amp; 0xFF
      return num
  
  data = [0x56, 0x54, 0x55, 0x3E, 0x28, 0x00, 0x08, 
          0x00, 0x03, 0x01, 0x46, 0x00, 0x00, 0x00, 0xC0]
  message = bytearray(data + [make_checksum(data)])
  </code></pre>
  
  <p><strong>Javascript</strong></p>
  
  <pre><code>function checksum(data) {
      let res = 0x00
      for (let i = 0; i &lt; data.length; ++i) {
          res = (res + data[i]) &amp; 0xFF
      }
      return String.fromCharCode(res)
  }
  
  let data = new Int8Array([0x56, 0x54, 0x55, 0x3E, 0x28, 0x00,
             0x08, 0x00, 0x03, 0x01, 0x46, 0x00, 0x00, 0x00, 0xC0])
  let message = String.fromCharCode(...data) + checksum(data)
  </code></pre>
  
  <p>I think this might have something to do with the difference between <em>ascii</em> and <em>UTF</em>.</p>