I have to make a program where I have to convert numbers from the decimal system into another one (base 2 to 9), like in my first question.
But this time I have to make it using a recursive function. So I came with:
function cambiarBase(n,b: integer): string;
Begin
if n < b then
cambiarBase := inttostr(n)
else
cambiarBase := inttostr(n mod b) + cambiarBase(n div b, b);
End;
It takes 2 integer variables, 'n' being the decimal number and 'b' being the base, and returns a string with the converted number. At the button procedure I show the number in a TMemo
memo1.Lines.Add(cambiarBase(n,b));
The problem I have is: with the function the way it is gives the digits in the reverse order (e.g. 301 in base 9 is 364, but its shown as 463). But if I use a ReverseString function after the if statement then the number its shown in a different order (in the example, the number would be now 634).
But if I apply the ReverseString function at the memo1.Lines.Add (outside of the function) then it shows the correct convertion.
What I want is to know how can I make it return the correct order of the digits by the function itself.
The program compiles it without any errors.
Again, thanks for reading.
LeoAM