I believe all integers are 32 bits and I also believe that isn't documented anywhere. So, there's no such thing as a 16-bit integer in Brightscript. You need to convert your signed 16-bit, 2's complement, values to signed 32-bit values. Your positive values will go from 0 to &h7FFF (0 to 32767) and your negative values will go from &hFFFF to &h8000 (-1 to -32768). So if the most significant bit is a one then your 16-bit number is negative. For the 32-bit equivalent, the 17 most significant bits will all be ones for a negative number.
Here's the code:
for i = 0 to ba.Count() - 1 step 2
x = ba[i] + ba[i+1]*256
if x > 32767 then x = x + &hFFFF0000 ' if x is >= 32768 then it's really a 16-bit negative number
end for
To convert back from a 32-bit number that goes from -32768 to +32767 is trivial:
j = 0
for i = 0 to numvals - 1
ba[j] = x[i] and &hFF
ba[j+1] = (x[i] and &FF00)/256
j = j + 2
end for
Hope this helps!
-JT
Roku Community Streaming Expert
Help others find this answer and click "Accept as Solution."
If you appreciate my answer, maybe give me a Kudo.
I am not a Roku employee.