Created
July 13, 2015 22:13
-
-
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/
This file contains hidden or 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
| -- 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