First time here? Check out the FAQ!
THIS IS A TEST INSTANCE. Feel free to ask and answer questions, but take care to avoid triggering too many notifications.
0

LUA: byte to nibbles (low/high)

  • retag add tags

Hello,

I need to convert raw hex data: "10 55 55 59 93 09 22 f0" into string: "0f22903995555501" but how to extract high/low nibbles from individual byte ?

function userdata2bcd(buffer, offset, len)
  local bytearr = {}
  for i = 1, len do
    num = userdata2dec(buffer, offset+len-i, 1)
    highByte = ...
    lowByte  = ...
    bytearr[i] = tostring(lowByte) .. tostring(highByte)
  end
  return table.concat(bytearr)
end

Any suggestion is appreciated...

Thx, A!

sezb51's avatar
9
sezb51
asked 2021-09-07 13:49:21 +0000
edit flag offensive 0 remove flag close merge delete

Comments

add a comment see more comments

1 Answer

0

I am not a LUA expert. But in the good old days of 8 bit assemblers it was a matter of masking and bitshift action. That migh help you to search in the right direction.

A pointer like http://lua-users.org/wiki/BitwiseOper... might help there.

hugo.vanderkooij's avatar
76
hugo.vanderkooij
answered 2021-09-07 15:06:17 +0000, updated 2021-09-07 15:07:44 +0000
edit flag offensive 0 remove flag delete link

Comments

this looks indeed promising:

function userdata2bcd(buffer, offset, len)
  local bytearr = {}
  for i = 1, len do
    num = userdata2dec(buffer, offset+len-i, 1)
    highByte = bit.rshift(num, 4)
    lowByte  = num - highByte * 16
    bytearr[i] = string.format("%.1X", lowByte) .. string.format("%.1X", highByte)
  end
  return table.concat(bytearr)
end

it does work indeed.

Thx,

sezb51's avatar sezb51 (2021-09-07 17:28:16 +0000) edit
add a comment see more comments

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss.

Add Answer