LLJS icon indicating copy to clipboard operation
LLJS copied to clipboard

Downcasting of Signed Types Not Working

Open Danielku15 opened this issue 12 years ago • 0 comments

If you try to downcast any integer to a lower byte signed integer the output is wrong.

trace("i16: " + (i16)(65535));

Leads to "Unknown expression type: undefined". It works as long the casted integer is in range of the int16.

A solution could be to write the unsigned version of the target integer into a ArrayBuffer using the according view, then read it again from the target type view.

var castBuffer = new ArrayBuffer(2);
var i16 = new Int16Array(castBuffer);
var u16 = new Uint16Array(castBuffer);
var i8 = new Int8Array(castBuffer);
var u8 = new Uint8Array(castBuffer);

exports.toInt16 = function (i) { u16[0] = i & 65535; return i16[0]; }
exports.toInt8 = function (i) { u8[0] = i & 255; return i8[0]; }

Downcasting float64 to float32 could be hard. I don't have a solution for this yet.

Danielku15 avatar Jan 25 '13 06:01 Danielku15