Custom codec: Decode int16


#1

I’m trying to use a statement like this one:

new Int16Array(1)

in my Decode function but I get an error saying:

js vm error: ReferenceError: ‘Int16Array’ is not defined


#2

IntArray16 was probably introduced in ES6 or later and the javascript VM that runs the decoder targets ES5. So, in short, it is indeed undefined.


#3

Thx for your answer. Then I wrote this ugly code to convert two bytes (in BigEndian) to a Javascript number:

function beBytesToShort(bytes) {
    var res = 0;
    if (bytes[0] & 0x80) {
      res = ~(((bytes[0] & 0xFF) << 8) | (bytes[1] & 0xff) | 0xffff0000) + 1;
      return -res;
    } else {
      res = ((bytes[0] & 0xFF) << 8) | (bytes[1] & 0xff);
      return res;
    }
}