n - (required) the number to be converted, as a string. It may be a number instead, if the input base is 10.
base - (required) the base to which the number should be converted. May be between 2 and 36, inclusive.
from - the base of the input. Defaults to 10 (or 16 if the input has a leading '0x'). Note that bases other than 10 are not supported if the input has a fractional part.
precision - number of digits to be rendered after the radix point. Trailing zeros will be added if needed. If not specified, however many digits are needed will be shown, up to 10.
width - minimum number of digits to be rendered before the radix point. Leading zeros will be added if needed.
default - Value to return if n is empty or non-numeric. Defaults to the value of n.
prefix /suffix - wikitext to add before/after the returned result. Will not be added if n is empty or non-numeric. For example, you might use a prefix of0x when converting to hex, or a suffix of<sub>8</sub> when converting to octal.
From templates
In wikimarkup, this module may be called with a function namentom, e.g.:
Markup
Renders as
{{#invoke:BaseConvert|16to10| FF }}
255
{{#invoke:BaseConvert|10to36|500}}
DW
{{#invoke:BaseConvert|10to16|Foo|default=0}}
0
All options above are supported, excluding|base=,|from= and|n= which are set by the mandatory options.
localp={}localdigits='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'localfunctionnormalizeFullWidthChars(s)returnmw.ustring.gsub(s,'[!-~]',function(s)returnmw.ustring.char(mw.ustring.codepoint(s,1)-0xFEE0)end)endlocalfunction_convert(n,base,from,precision,width,default,prefix,suffix)n=tostring(n)-- strip off any leading '0x' (unless x is a valid digit in the input base)from=tonumber(from)ifnotfromorfrom<34thenlocalcn,c=n:gsub('^(-?)0[Xx]','%1')ifc>0andnotfromthenfrom=16endend-- check for a negative sign. Do this while the input is still in string form,-- because tonumber doesn't support negative numbers in non-10 bases.localsign=''localcn,c=n:gsub('^-','')ifc>0thensign='-'end-- replace any full-width Unicode characters in the string with their ASCII equivalentsn=normalizeFullWidthChars(n)-- handle scientific notation with whitespace around the 'e' e.g. '5 e7'n=n:gsub('%s*[eE]%s*','e')from=fromor10localnum=tonumber(n,from)base=tonumber(base)precision=tonumber(precision)width=tonumber(width)ifnotnumornotbasethenreturndefaultornendlocali,f=math.modf(num)localt={}repeatlocald=(i%base)+1i=math.floor(i/base)table.insert(t,1,digits:sub(d,d))untili==0while#t<(widthor0)dotable.insert(t,1,'0')endlocalintPart=table.concat(t,'')-- compute the fractional partlocaltf={}whilef>0and#tf<(precisionor10)dof=f*basei,f=math.modf(f)table.insert(tf,digits:sub(i+1,i+1))end-- add trailing zeros if neededifprecisionand#tf<precisionthenfori=1,precision-#tfdotable.insert(tf,'0')endendlocalfracPart=table.concat(tf,'')-- remove trailing zeros if not neededifnotprecisionthenfracPart=fracPart:gsub('0*$','')end-- add the radix point if neededif#fracPart>0thenfracPart='.'..fracPartendreturn(prefixor'')..sign..intPart..fracPart..(suffixor'')endfunctionp.convert(frame)-- Allow for invocation via #invoke or directly from another modulelocalargsifframe==mw.getCurrentFrame()thenargs=frame.argselseargs=frameendlocaln=args.nlocalbase=args.baselocalfrom=args.fromlocalprecision=args.precisionlocalwidth=args.widthlocaldefault=args.defaultlocalprefix=args.prefixlocalsuffix=args.suffixreturn_convert(n,base,from,precision,width,default,prefix,suffix)endsetmetatable(p,{__index=function(t,k)localfrom,base=k:match('^([0-9]+)to([0-9]+)$')ifnotfromthenreturnnilendreturnfunction(frame)localargs=frame.argsreturn_convert(mw.text.trim(args[1]),base,from,args.precision,args.width,args.default,args.prefix,args.suffix)endend})returnp