Skip to content

Instantly share code, notes, and snippets.

Created Sep 24, 2016
What would you like to do?
Pico8 number-to-hex function
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
return str
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment