![]() |
VOOZH | about |
This module allows any number base to be converted to any other number base up to base 15.
It provides 3 convenience functions: fromDec, fromHex, and fromBin, for converting from decimal, hexadecimal, and binary, to any other base; and one main function: fromBase, for everything else.
{{Decimal to binary converter}}{{Decimal to hexadecimal converter}}{{Hexadecimal to decimal converter}}localp={} functionp.fromDec(f) localargs=f.argsorf localbase=tonumber(args[2])or16 localdec=tonumber(args[1])or0 ifbase==10ordec==0then returndec elseifbase==16then returnstring.format('%X',dec) else localchars='0123456789ABCDEF' localpos localout='' whiledec>0do pos=math.mod(dec,base)+1 dec=math.floor(dec/base) out=string.sub(chars,pos,pos)..out end returnout end end functionp.fromBase(f) localargs=f.argsorf localfromBase=tonumber(args[3])or10 localtoBase=tonumber(args[2])or16 localdec=tonumber(args[1],fromBase)or0 returnp.fromDec{dec,toBase} end functionp.fromHex(f) returnp.fromBase{f.args[1],f.args[2],16} end functionp.fromBin(f) returnp.fromBase{f.args[1],f.args[2],2} end returnp