|
| 1 | +import java.util.Arrays; |
| 2 | +import java.util.HashSet; |
| 3 | +import java.util.InputMismatchException; |
| 4 | +import java.util.Scanner; |
| 5 | + |
| 6 | +/** |
| 7 | + * Class for converting from "any" base to "any" other base, when "any" means from 2-36. |
| 8 | + * Works by going from base 1 to decimal to base 2. Includes auxiliary method for |
| 9 | + * determining whether a number is valid for a given base. |
| 10 | + * |
| 11 | + * @author Michael Rolland |
| 12 | + * @version 2017.10.10 |
| 13 | + * |
| 14 | + */ |
| 15 | +public class AnyBaseToAnyBase { |
| 16 | + |
| 17 | + // Smallest and largest base you want to accept as valid input |
| 18 | + static final int MINIMUM_BASE = 2; |
| 19 | + static final int MAXIMUM_BASE = 36; |
| 20 | + |
| 21 | + // Driver |
| 22 | + public static void main(String[] args) { |
| 23 | + Scanner in = new Scanner(System.in); |
| 24 | + String n; |
| 25 | + int b1=0,b2=0; |
| 26 | + while (true) { |
| 27 | + try { |
| 28 | + System.out.print("Enter number: "); |
| 29 | + n = in.next(); |
| 30 | + System.out.print("Enter beginning base (between "+MINIMUM_BASE+" and "+MAXIMUM_BASE+"): "); |
| 31 | + b1 = in.nextInt(); |
| 32 | + if (b1 > MAXIMUM_BASE || b1 < MINIMUM_BASE) { |
| 33 | + System.out.println("Invalid base!"); |
| 34 | + continue; |
| 35 | + } |
| 36 | + if (!validForBase(n, b1)) { |
| 37 | + System.out.println("The number is invalid for this base!"); |
| 38 | + continue; |
| 39 | + } |
| 40 | + System.out.print("Enter end base (between "+MINIMUM_BASE+" and "+MAXIMUM_BASE+"): "); |
| 41 | + b2 = in.nextInt(); |
| 42 | + if (b2 > MAXIMUM_BASE || b2 < MINIMUM_BASE) { |
| 43 | + System.out.println("Invalid base!"); |
| 44 | + continue; |
| 45 | + } |
| 46 | + break; |
| 47 | + } catch (InputMismatchException e) { |
| 48 | + System.out.println("Invalid input."); |
| 49 | + in.next(); |
| 50 | + } |
| 51 | + } |
| 52 | + System.out.println(base2base(n, b1, b2)); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Checks if a number (as a String) is valid for a given base. |
| 57 | + */ |
| 58 | + public static boolean validForBase(String n, int base) { |
| 59 | + char[] validDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', |
| 60 | + 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', |
| 61 | + 'W', 'X', 'Y', 'Z'}; |
| 62 | + // digitsForBase contains all the valid digits for the base given |
| 63 | + char[] digitsForBase = Arrays.copyOfRange(validDigits, 0, base); |
| 64 | + |
| 65 | + // Convert character array into set for convenience of contains() method |
| 66 | + HashSet<Character> digitsList = new HashSet(); |
| 67 | + for (int i=0; i<digitsForBase.length; i++) |
| 68 | + digitsList.add(digitsForBase[i]); |
| 69 | + |
| 70 | + // Check that every digit in n is within the list of valid digits for that base. |
| 71 | + for (char c : n.toCharArray()) |
| 72 | + if (!digitsList.contains(c)) |
| 73 | + return false; |
| 74 | + |
| 75 | + return true; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Method to convert any integer from base b1 to base b2. Works by converting from b1 to decimal, |
| 80 | + * then decimal to b2. |
| 81 | + * @param n The integer to be converted. |
| 82 | + * @param b1 Beginning base. |
| 83 | + * @param b2 End base. |
| 84 | + * @return n in base b2. |
| 85 | + */ |
| 86 | + public static String base2base(String n, int b1, int b2) { |
| 87 | + // Declare variables: decimal value of n, |
| 88 | + // character of base b1, character of base b2, |
| 89 | + // and the string that will be returned. |
| 90 | + int decimalValue = 0, charB2; |
| 91 | + char charB1; |
| 92 | + String output=""; |
| 93 | + // Go through every character of n |
| 94 | + for (int i=0; i<n.length(); i++) { |
| 95 | + // store the character in charB1 |
| 96 | + charB1 = n.charAt(i); |
| 97 | + // if it is a non-number, convert it to a decimal value >9 and store it in charB2 |
| 98 | + if (charB1 >= 'A' && charB1 <= 'Z') |
| 99 | + charB2 = 10 + (charB1 - 'A'); |
| 100 | + // Else, store the integer value in charB2 |
| 101 | + else |
| 102 | + charB2 = charB1 - '0'; |
| 103 | + // Convert the digit to decimal and add it to the |
| 104 | + // decimalValue of n |
| 105 | + decimalValue = decimalValue * b1 + charB2; |
| 106 | + } |
| 107 | + |
| 108 | + // Converting the decimal value to base b2: |
| 109 | + // A number is converted from decimal to another base |
| 110 | + // by continuously dividing by the base and recording |
| 111 | + // the remainder until the quotient is zero. The number in the |
| 112 | + // new base is the remainders, with the last remainder |
| 113 | + // being the left-most digit. |
| 114 | + |
| 115 | + // While the quotient is NOT zero: |
| 116 | + while (decimalValue != 0) { |
| 117 | + // If the remainder is a digit < 10, simply add it to |
| 118 | + // the left side of the new number. |
| 119 | + if (decimalValue % b2 < 10) |
| 120 | + output = Integer.toString(decimalValue % b2) + output; |
| 121 | + // If the remainder is >= 10, add a character with the |
| 122 | + // corresponding value to the new number. (A = 10, B = 11, C = 12, ...) |
| 123 | + else |
| 124 | + output = (char)((decimalValue % b2)+55) + output; |
| 125 | + // Divide by the new base again |
| 126 | + decimalValue /= b2; |
| 127 | + } |
| 128 | + return output; |
| 129 | + } |
| 130 | +} |
0 commit comments