Skip to content

Instantly share code, notes, and snippets.

@TechplexEngineer
Created July 13, 2015 22:13
Show Gist options
  • Select an option

  • Save TechplexEngineer/fc5e849655243f7dc8d6 to your computer and use it in GitHub Desktop.

Select an option

Save TechplexEngineer/fc5e849655243f7dc8d6 to your computer and use it in GitHub Desktop.
Print a number in its hexadecimal format. Based on http://snipplr.com/view/13086/number-to-hex/
-- convert a number to its hex representation
function num2hex(num, hexdigits)
local hexstr = '0123456789abcdef'
local s = ''
while num > 0 do
local mod = math.fmod(num, 16)
s = string.sub(hexstr, mod+1, mod+1) .. s
num = math.floor(num / 16)
end
if s == '' then s = '0' end
while s:len() < hexdigits do
s = "0" .. s
end
return s
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment