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