This code is part of a project that I have to develop. I need to write a java method that converts a number (base 10) to another number in the database n. This is the class code:
public class converter {
public int from_10_to_n(int base, int newbase) {
int k = 0;
String res = "";
String output = "";
do {
base /= newbase;
k = base % newbase;
res += String.valueOf(k);
} while (base != 0);
for(int i=res.length()-1; i>-1; i--) {
output += res.charAt(i);
}
return Integer.parseInt(output);
}
I thought to make the program this way:

The cycle do {} while();divides the numbers and stores the remainders in k. Then I create a for loop that changes the res line (which has reminders).
By the way, when I call the method in my main, I miss the last digit. I mean:
converter k = new converter();
int e = k.from_10_to_n(a , b );
System.out.println(a+" -> "+b+" is " + e);
With this code, if I want to convert 231 to base 4, I get 321as a result instead 3213. I checked my code, but I can not find a solution. Any idea?
. , 31 ( 10) 11111 ( 2), 1111.