0

I am trying to figure out how to convert any value in a specific base to its corresponding representation in a different base. You are given a value, its base, and the base the value needs to be converted to in each line in a .dat file.

Example of 3 lines in the .dat file where the first number in a line is the value, followed by the base it's in, then the base it needs to be converted to:

10 10 2

AB 16 10

345 6 4

I believe I have figured it out except for how to account for letter values in bases such as 16 an 18. Please help:

public static void main( String args[] ) throws IOException
    {
        Scanner file = new Scanner(new File("baseConversion.dat"));
        //Keep looping while you can still read lines from the file
        while (file.hasNextLine())
        {
            //Read a line from the file -- read it as a string
            String line = file.nextLine();

            int firstSpace = line.indexOf(" ");
            int lastSpace = line.lastIndexOf(' ', line.length());
            String oldBase = line.substring(firstSpace + 1, lastSpace); 


            // Get the new base from the string
            int newBase = Integer.parseInt(line.substring(lastSpace + 1));


            //  Convert from the Old base to base 10
            int numberInBase10 = convertToTen(line.substring(0, firstSpace), Integer.parseInt(oldBase));
            System.out.println("Converted " + line.substring(0, firstSpace) + " from base " + oldBase + " to " +
            numberInBase10 + " in base 10\n");

            public static String convert(int numberInBase10, int base)
            {
            int quotient = numberInBase10 / newBase;
            int remainder = numberInBase10 % newBase;

            if(quotient == 0)
            {
                return Integer.toString(remainder);      
            }
            else
            {
                return convert(quotient, newBase) + Integer.toString(remainder);
            }
        }

        // DO THIS Print out results


        }
        file.close();
    }


    static int convertToTen(String num, int base)
    {

    int leng = num.length();
    int base10 = 0;
    int remainder = 0;
    int add = 0;


        for(int i = leng-1; i >=0; i--)
        {
            int intValueForC = 0;
            remainder = 0;

            char c = num.charAt(leng-i-1);
            if(!Character.isDigit(c))
            {
                intValueForC = c - 55;
            }
            else
            {
                intValueForC = c - 48;
            }
            add +=(intValueForC * Math.pow(base, i));

        }

        return add;
    }
}
dckuehn
  • 2,427
  • 3
  • 27
  • 37
user2880377
  • 1
  • 1
  • 2
  • It pretty much is except I figured out the part I was asking for in my old question and now I have a new question. – user2880377 Oct 14 '13 at 22:56
  • Something along these lines: `if (digit >= base) { digit = 'A' + (digit - base)};` when translating into 16 or 18 base. Just an idea. – PM 77-1 Oct 14 '13 at 23:34

1 Answers1

2

You're massively over-complicating this (unless this is homework and you're not allowed to use library functions).

String hex = "ABCDEF";
int decimal = Integer.parseInt(hex, 16);
String binary = Integer.toString(decimal, 2);
Dan Dyer
  • 53,737
  • 19
  • 129
  • 165