This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Byte by Byte Decoding
#1
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,
Reply
#2
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
Reply


Forum Jump: