You're simply adding the end of the newNum
instead of prepending.
newNum = newNum + orig%newBase;
... should be ...
newNum = orig%newBase + newNum;
For characters...
When orig%newBase > 9
, then char(55 + orig%newBase)
var nextValue = orig % newBase;
if (nextValue > 9)
{
newNum = char(55 + nextValue) + newNum;
}
else
{
newNum = nextValue + newNum;
}