Logic Machine Forum
3 bytes to 2 bytes - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: 3 bytes to 2 bytes (/showthread.php?tid=5215)



3 bytes to 2 bytes - savaskorkmaz - 23.01.2024

Hi ,

I need to get value of the last 2 bytes of a 3 bytes object. I had a function for converting byte to byte shown below. I need to get byte 1 and byte 2 ( Not 0 ) as single variable.

Thanks

value = event.getvalue()
function getbyte(value, byte)
  return bit.band(bit.rshift(value, byte * 8), 0xFF)
end


b0 = getbyte(value, 0)
b1 = getbyte(value, 1)
b2 = getbyte(value, 2)


RE: 3 bytes to 2 bytes - admin - 23.01.2024

You can multiply one byte by 256 then add the other one:
Code:
b = b1 * 256 + b2

You might need to change which bytes to use and in what order, depending on how the source value is encoded.