Skip to content

Instantly share code, notes, and snippets.

@craibuc
Last active December 30, 2015 10:19
Show Gist options
  • Select an option

  • Save craibuc/7815332 to your computer and use it in GitHub Desktop.

Select an option

Save craibuc/7815332 to your computer and use it in GitHub Desktop.
Convert a base-10 number to a base-N number.
Function (Numbervar base_ten, Numbervar base_n)
// quotient
Local Numbervar q;
// remainder
Local Numbervar r;
// converted values
Local Numbervar Array slots;
do (
// integer division
q:= base_ten \ base_n;
// remainder
r:= remainder(base_ten, base_n);
// resize array, preserving content
redim preserve slots[ubound(slots)+1];
// store new value
slots[ubound(slots)]:=r;
// reassign value
base_ten:=q
) while q>0;
Local Stringvar result;
Local Numbervar i;
for i:= Ubound(slots) to 1 step -1 do (
// for base numbers large than 10, assign a letter (e.g. base-16)
if slots[i]>9 then
// assign a letter
result := result + Chr(65+(slots[i]-9)-1)
else
// use the numeric value (no formatting)
result := result + ToText(slots[i],"#")
);
// return
result;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment