Question prompt: Modify Binary to get a program that takes two integer command-line arguments i and k and converts i to base k. Assume that i is an integer in Java’s long data type and that k is an integer between 2 and 16. For bases greater than 10, use the letters A through F to represent the 11th through 16th digits, respectively.
I have managed to convert decimals to base 2-10 successfully, but I can't seem to convert decimals with the inclusion of the characters A through F that represents the 11th to 16th digits, respectively.
I don't want to use any arrays or lists. I understand my switch statement is lacking functionality that would solve my problem, but I'm not sure how to improve it. I'm trying to solve the problem using a switch statement.
In the code, i represents an integer to convert to base k. k represents a base that can be 2-16.
public class BaseConversion
{
public static void main(String[] args)
{
int i = Integer.parseInt(args[0]);
int k = Integer.parseInt(args[1]);
int power = 1;
while (power <= i/k)
{
power *= k;
} // Now power is the largest power of k <= i.
if (k <= 10)
{
while (power > 0)
{
int digit = i / power;
System.out.print(digit);
i -= digit * power;
power /= k;
}
}
else if (k > 10)
{
switch (k)
{
case 11: System.out.println('A'); break;
case 12: System.out.println('B'); break;
case 13: System.out.println('C'); break;
case 14: System.out.println('D'); break;
case 15: System.out.println('E'); break;
case 16: System.out.println('F'); break;
}
}
}
}