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.

3 bytes to 2 bytes
#1
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)
Reply
#2
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.
Reply


Forum Jump: