vitoralmeidasilva [Lexaloffle Blog Feed]https://www.lexaloffle.com/bbs/?uid=24178 Converting number to hexadecimal <p>Here is a simple function to convert a number to its hexadecimal string representation:</p> <div> <div class=scrollable_with_touch style="width:100%; max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre> function num2hex(number) local base = 16 local result = {} local resultstr = &quot;&quot; local digits = &quot;0123456789abcdef&quot; local quotient = flr(number / base) local remainder = number % base add(result, sub(digits, remainder + 1, remainder + 1)) while (quotient &gt; 0) do local old = quotient quotient /= base quotient = flr(quotient) remainder = old % base add(result, sub(digits, remainder + 1, remainder + 1)) end for i = #result, 1, -1 do resultstr = resultstr..result[i] end return resultstr end </pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p>Usage:</p> <div> <div class=scrollable_with_touch style="width:100%; max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre> print(num2hex(255)) -- ff print(num2hex(10)) -- a print(num2hex(1050)) -- 41a </pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> https://www.lexaloffle.com/bbs/?tid=30910 https://www.lexaloffle.com/bbs/?tid=30910 Wed, 07 Mar 2018 22:44:31 UTC