-1

So for a homework assignment we had to make a program that converted a number from one base to another (i.e. 110 in base 2 to 6 in base 10). I asked my friend how he did his because I was having trouble and he just sent me his code and nothing else. Can someone explain the logic of this code so that I can make my own program and actually understand how to do this problem. Thanks!

import java.util.*;
public class Base_Converter {
    public static final String value = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    public static void main(String args[]){
    int x, y;
    String num, base10 = "";
    Scanner scan = new Scanner(System.in);

        System.out.println("Enter a number you want to convert.");
        num = scan.nextLine();
        num = num.toUpperCase();
        System.out.println("What base is it in?");
        x = scan.nextInt();
        System.out.println("What base do you want to convert it to?");
        y = scan.nextInt();
        if(x <= 36 && y <= 36 && x > 1 && y > 1){
        base10 = toBase10(num,x);
        num = newBase(base10,y);
        System.out.println(num);
        }
    }
    public static String toBase10(String num, int from){
        long total = 0;
        int counter = num.length();
        char[] stringArray = num.toCharArray();
        for(char w : stringArray){
            counter--;
            total += value.indexOf(w)*Math.pow(from,counter);
        }
        return String.valueOf(total);
    }
    public static String newBase(String num, int to){
        String total = "";
        int current = 0;
        while(Integer.valueOf(num) > 0){
            current = Integer.valueOf(num)%to;
            total = value.charAt(current)+total;
            num = String.valueOf(Integer.valueOf(num)/to);
        }
    return total;
    }
}
user2543577
  • 55
  • 1
  • 5

1 Answers1

0

I think you should be focusing not on what your friend's code does, but instead with how to do the assignment yourself, because I think your problems lie with a lack of understanding on your part. Instead of leaving you high and dry though I'll walk you through some of the specifics of base-conversion.

First, read user input. It looks like you're using Java, so just use a scanner to do this. At minimum you'll want to read the number you're converting, what base it is in, and what base the output will be in.

Next, we want to convert the number. You could directly convert numbers to each other (i.e. converting base 2 to base 8) but that requires more brainpower than I am willing to offer right now. Instead, I would suggest always first converting the user-inputted number to base 10 (much like your friend did). So how do we convert a number of an unknown base to base 10?

So let's break down how a number is represented: lets say we have the number 234 in base ten. This is equivalent to 4*10^0 + 3*10^1 + 2*10^2 or 4 + 30 + 200 = 234. You can use this same conversion for any other numbers. I.E. if the number is 1763 in base 8, the value in base 10 will be 3*8^0 + 6*8^1 + 7*8^2 + 1*8^3 or 3 + 48 + 448 + 512 = 1011 base 10(try entering 1763 here for proof. So to convert to decimal, you just need to see to multiply each individual number time your base to the power of its place minus 1. For example, since 1 is the fourth number in the 1763 you multiply it times 8^(4-1). Since, you are reading a string from the user. You'll need to convert each character of the string to an integer using the ascii chart.

Now to convert from base ten to anything. Instead of multiplying, you just divide each value and write the remainder! I'll let someone else describe this procedure.

Now just store this new value as a string doing somethings like

String output = "";
output += newValue;

In computer science, just copying someone else's code is way more harmful than helpful. Hope this helps!

Community
  • 1
  • 1
bpgeck
  • 1,440
  • 9
  • 25