Log In  

@vitoralmeidasilva

Follow
Follow

Here is a simple function to convert a number to its hexadecimal string representation:

function num2hex(number)
    local base = 16
    local result = {}
    local resultstr = ""

    local digits = "0123456789abcdef"
    local quotient = flr(number / base)
    local remainder = number % base

    add(result, sub(digits, remainder + 1, remainder + 1))

  while (quotient > 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

Usage:

    print(num2hex(255)) -- ff
    print(num2hex(10)) -- a
    print(num2hex(1050)) -- 41a
P#50079 2018-03-07 22:44 ( Edited 2018-03-08 19:41)

About | Contact | Updates | Terms of Use
Follow Lexaloffle:        
Generated 2019-08-25 12:21 | 0.066s | 2097k | Q:10