In: Computer Science
Design and implement a method that will take any value as String (even it's a number!) and convert that value to a number in any base. Use the method;
private static ArrayList convertToBase(String value, int currentBase, int targetBase){
// your algorithm
return result; //which is a String array
}
Examples:
convertToBase("10011", 2, 10) should return [1, 9], meaning,
(10011)base2 = (19)base10
convertToBase("100", 10, 8) should return [6, 4]
convertToBase("E12B0", 16, 2) should return [1,1,1,0,0,0,0,1,0,0,1,0,1,0,1,1,0,0,0,0]
convertToBase("1250", 10, 16) should return [4, E, 2]
Write a tester, make sure your code produces correct results.
i am using blue j / java, can you please provide a solution for the above with explaining comments
thank you
NOTE:If you are stuck somewhere feel free to ask in the comments....I will try to help you out...but do not give negative rating to the question as it affects my answering rights........
Also note the code is explained in comments.....walk carefully through the code to understand the working
Iam assuming you know the basic conversions from one base to another...
//NOTE the idea is simple
//First we see that if both the source and the destination base are the same , if so no need to convert
//next step is probably the most important
//we need to change the given string to decimal format if not in the decimal base
//this is done by calling getDecimal() function
//once we convert it to decimal the idea is simple of dividing the decimal no by required base to get remainder
//check that remainder with appropriate conditions
//and then move on to next iteration
import java.io.*;
import java.util.*;
public class BasetoBase{
public static void main(String args[])throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the number: ");
String n = br.readLine();
n = n.toUpperCase();
System.out.print("Source base: ");
int s = Integer.parseInt(br.readLine());
System.out.print("Destination base: ");
int d = Integer.parseInt(br.readLine());
ArrayList<String> res = convertToBase(n, s, d);
System.out.println(res);
}
public static ArrayList convertToBase(String n, int b1, int b2){
if(b1 == b2){//if base is same
ArrayList<String> result=new ArrayList<String>();
for(int i=0;i<n.length();i++){
char c=n.charAt(i);
result.add(Character.toString(c));
}
return result;
}
int deci = 0;
if(b1 != 10)
deci = getDecimal(n, b1);//if source base is not in decimal
else
deci=Integer.parseInt(n);//if already in decimal just convert to int format
ArrayList<String> result=new ArrayList<String>();
if(deci == 0)
result.add("0");
while(deci != 0){
int d = deci % b2;//gets the remainder
//now check for the following cases ....this might be the cases when converting to hexadecimal
if(d < 10)
result.add(0,Integer.toString(d));
else if(d == 10)
result.add(0,"A");
else if(d == 11)
result.add(0,"B");
else if(d == 12)
result.add(0,"C");
else if(d == 13)
result.add(0,"D");
else if(d == 14)
result.add(0,"E");
else if(d == 15)
result.add(0,"F");
deci /= b2;//before moving to next iteration update the decimal no
}
return result;//Arraylist storing the result
}
public static int getDecimal(String n, int base){
int d = 0;
int num = 0;
int p = 0;
for(int i = n.length() - 1; i >= 0; i--){
char ch = n.charAt(i);
switch(ch){
case '0':
num = 0;
break;
case '1':
num = 1;
break;
case '2':
num = 2;
break;
case '3':
num = 3;
break;
case '4':
num = 4;
break;
case '5':
num = 5;
break;
case '6':
num = 6;
break;
case '7':
num = 7;
break;
case '8':
num = 8;
break;
case '9':
num = 9;
break;
case 'A':
num = 10;
break;
case 'B':
num = 11;
break;
case 'C':
num = 12;
break;
case 'D':
num = 13;
break;
case 'E':
num = 14;
break;
case 'F':
num = 15;
break;
}
d += num * (int)Math.pow(base, p);
p++;
}
return d;
}
}
Outputs:--->