Created
September 24, 2016 13:45
-
-
Save KeyMaster-/7aabc3697bcd9a1a490763c7efd7334a to your computer and use it in GitHub Desktop.
Pico8 number-to-hex function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
hex_abc = '0123456789abcdef' | |
function to_hex(num) | |
local str = '' | |
for i=0,7 do --8 packs of 4 bits for 32 bit number | |
local digit = num | |
local shift = (i - 4)*4 | |
if shift < 0 then digit = shl(digit, -shift) | |
else digit = shr(digit, shift) end | |
digit = band(digit, 0xf) --cut away any bits outside what we're looking at (especially important for repeated sign bit from right shift at i=7) | |
str = sub(hex_abc, digit+1, digit+1) .. str | |
end | |
return str | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment