Logic Machine Forum
Byte by Byte Decoding - 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: Byte by Byte Decoding (/showthread.php?tid=2969)



Byte by Byte Decoding - savaskorkmaz - 11.11.2020

Hi there,

In forum, there are examples for byte and bit decoding. 

I need to decode 3 byte information to 3 different byte as a 3 different point. In my example, my 3 bytes point value is 46124.
If we decode, 
my first byte value will be 44
my second byte value will be 180 and
my third byte value will be 0

as 3 different points. I used a script like that and it didn't worked. Can you help me about it ?

value = event.getvalue()

bytes = { value:byte(1, #value) }

grp.checkupdate('32/1/1', bytes[ 1 ])
grp.checkupdate('32/1/2', bytes[ 2 ])
grp.checkupdate('32/1/3', bytes[ 3 ])

Thanks,


RE: Byte by Byte Decoding - admin - 12.11.2020

For numbers up to 4 bytes you can use this function:
Code:
function getbyte(value, byte)
  return bit.band(bit.rshift(value, byte * 8), 0xFF)
end

value = 46124

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