Module:Math
Uiterlijk
(MediaWiki:Scribunto-doc-page-show)
Functies:
- decimalToHex() - geeft de hexadecimale waarde van een decimaal getal; gebruik en toelichting via Sjabloon:Dec2Hex
- hex2Decimal() - geeft de decimale waarde van een positief hexadecimaal getal; gebruik en toelichting via Sjabloon:Hex2Dec
local p={}
function p.decimalToHex(frame)
-- bron: https://stackoverflow.com/questions/37796287/convert-decimal-to-hex-in-lua-4
local hexstr = "0123456789ABCDEF"
local neg = false
local result = ""
num = tonumber(frame.args[1])
if num == 0 then
return '0'
end
if num < 0 then
neg = true
num = num * -1
end
while num > 0 do
local n = math.mod(num, 16)
result = string.sub(hexstr, n + 1, n + 1) .. result
num = math.floor(num / 16)
end
if neg then
result = '-' .. result
end
return result
end
function p.hex2Decimal(frame)
-- if frame.args[1] == nil then return p._error('hex2Decimal', 'waarde ontbreekt)') end
-- if p._asc(frame.args[1]) == 45 then return p._error('hex2Decimal', 'negatieve waarde') end
-- local x = tonumber(frame.args[1], 10)
-- if x < 0 then return p._error('hex2Decimal', 'negatieve waarde') end
return tonumber(frame.args[1], 16)
end
function p._asc(frame)
return string.byte(frame.args[1])
end
function p._error(fn, txt)
return '<strong class="error">Fout in Math:' .. fn .. '(): ' .. txt .. '</strong>'
end
return p