Skip to content

Instantly share code, notes, and snippets.

@max1220
Created May 28, 2018 14:36
Show Gist options
  • Save max1220/719b9472cef49398f0d67912947b72df to your computer and use it in GitHub Desktop.
Save max1220/719b9472cef49398f0d67912947b72df to your computer and use it in GitHub Desktop.
Debug function that returns a lower n bytes from a numer as hexadecimal, binary and decimal.
local function num_to_str(num, bytes)
local num = bit.band(num, 2^(bytes*8) - 1) -- limit to selected bytes
local bits = {}
for i=bytes*8-1, 0, -1 do -- from highest bit to lowest
local bit = (bit.band(num, 2^i) == 0) and 0 or 1 -- check if the current bit is set
table.insert(bits, bit) -- append to bits table
end
return ("0x%.4X 0b%s 0d%d"):format(num, table.concat(bits), num)
end
return num_to_str
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment