haXe Forum > Memory.getUI16() but I want an int

  • I'm adventuring into the world of flash.Memory and it's great fun

    However, I'm running into some trouble and confusion, because while I can set Int with setI16, when I'm to get things, all I have is getUI16 which doesn't give me the same number back :s

    So, yeah, pretty stuck here.

  • Your right about having no getI16() in the Memory API, maybe you could use a 32bit integer instead.
     
    An Unsigned Int would be read differently from a regular Integer, because they have no negative values, but allow for twice the max number.
     
    If you really want you could probably convert it or read 2 bytes(16 bits) and join them together using bitwise operations, but seems more trouble/performance then its worth. (even though bitwise operations are fast)

    # | & ^ : perform bitwise operations between two Int expressions. Returns Int.
    # << >> >>> : perform bitwise shifts between two Int expressions. Returns Int.

    What baffles me, is why haxe has an api where we only have setI16 and getUI16 with no matching get and set of the matching type.

  • Yeah, I changed it to 32 and it works, but it bothers me a bit since I'm now using 8 bytes for every value.

  • You can convert an unsigned Int to a signed one by sign extension:

    var signedVal = Memory.signExtend16(Memory.getUI16(address))

    (Similarly, signExtend8 does the same for bytes.)

    As to why the Memory class is constructed this way, the simple answer is that it follows the Alchemy opcodes: each method corresponds with one Alchemy opcode, and there's no single opcode to get a signed short. (Nothing's stopping you extending the Memory class to add the extra functions, though!)

< Prev | Page 1 / 1 | Next >