Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a program that converts from one base (2-36) to another Asks the user for

ID: 3844838 • Letter: C

Question

Create a program that converts from one base (2-36) to another

Asks the user for a number - your program should read this in as a string/text...not as an integer (to account for alpha characters).

Ask for the initial number's base (doesn't matter how it is read in):

Ask for the target number's base (doesn't matter how it is read in):

The program should output the converted number.

Please write program in Java. (I am placing an arbitrary restriction on the symbols for digits 0-9A-Z...you could theoretically go even higher with your bases, but I haven't established which symbols to use.

Example Execution (Highlighting is showing the user input):

  Enter a number: AZ  
  Enter the number's base: 36   
  Enter the target radix: 5    The number in base-5 is 3040  
       

Explanation / Answer

It is a very simple program, you could just use parseInt() and toString() function to do it. What parseInt() does is takes an string argument and passes a integer of the given radix. and we use the toString to convert the given integer to string value. Have a look at the code:

import java.util.*;
import java.lang.*;
import java.io.*;

class baseConverter
{
   public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number: ");
String iNumber = scanner.next(); //takes the string value
System.out.println("Enter the number's base: ");
int nBase = scanner.nextInt(); //input's base
System.out.println("Enter the target radix: ");
int oBase = scanner.nextInt(); //output's base
System.out.println("The number in base-" + oBase + " is " +Integer.toString(Integer.parseInt(iNumber, nBase), oBase)); //the golden statement
}
}

I haven't added any constraints to check, they can be easily added. This can convert from any base to any base. If you have any doubts let me know.