Skip to content

Commit 22c48e0

Browse files
Create AnyBaseToAnyBase
Class for converting a number between any two bases, by going through decimal.
1 parent b21444d commit 22c48e0

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

Conversions/AnyBaseToAnyBase

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import java.util.InputMismatchException;
2+
import java.util.Scanner;
3+
4+
/**
5+
* Class for converting from any base to any other base, though it's unclear how digits greater than
6+
* 36 would be represented in bases >36.
7+
*
8+
* @author Michael Rolland
9+
* @version 2017.09.29
10+
*
11+
*/
12+
public class AnyBaseToAnyBase {
13+
14+
// Driver
15+
public static void main(String[] args) {
16+
Scanner in = new Scanner(System.in);
17+
System.out.print("Enter number: ");
18+
String n = in.nextLine();
19+
int b1=0,b2=0;
20+
while (true) {
21+
try {
22+
System.out.print("Enter beginning base: ");
23+
b1 = in.nextInt();
24+
System.out.print("Enter end base: ");
25+
b2 = in.nextInt();
26+
break;
27+
} catch (InputMismatchException e) {
28+
System.out.println("Invalid input.");
29+
in.next();
30+
}
31+
}
32+
System.out.println(base2base(n, b1, b2));
33+
}
34+
35+
/**
36+
* Method to convert any integer from base b1 to base b2. Works by converting from b1 to decimal,
37+
* then decimal to b2.
38+
* @param n The integer to be converted.
39+
* @param b1 Beginning base.
40+
* @param b2 End base.
41+
* @return n in base b2.
42+
*/
43+
public static String base2base(String n, int b1, int b2) {
44+
// Declare variables: decimal value of n,
45+
// character of base b1, character of base b2,
46+
// and the string that will be returned.
47+
int decimalValue = 0, charB2;
48+
char charB1;
49+
String output="";
50+
// Go through every character of n
51+
for (int i=0; i<n.length(); i++) {
52+
// store the character in charB1
53+
charB1 = n.charAt(i);
54+
// if it is a non-number, convert it to a decimal value >9 and store it in charB2
55+
if (charB1 >= 'A' && charB1 <= 'Z')
56+
charB2 = 10 + (charB1 - 'A');
57+
// Else, store the integer value in charB2
58+
else
59+
charB2 = charB1 - '0';
60+
// Convert the digit to decimal and add it to the
61+
// decimalValue of n
62+
decimalValue = decimalValue * b1 + charB2;
63+
}
64+
65+
// Converting the decimal value to base b2:
66+
// A number is converted from decimal to another base
67+
// by continuously dividing by the base and recording
68+
// the remainder until the quotient is zero. The number in the
69+
// new base is the remainders, with the last remainder
70+
// being the left-most digit.
71+
72+
// While the quotient is NOT zero:
73+
while (decimalValue != 0) {
74+
// If the remainder is a digit < 10, simply add it to
75+
// the left side of the new number.
76+
if (decimalValue % b2 < 10)
77+
output = Integer.toString(decimalValue % b2) + output;
78+
// If the remainder is >= 10, add a character with the
79+
// corresponding value to the new number. (A = 10, B = 11, C = 12, ...)
80+
else
81+
output = (char)((decimalValue % b2)+55) + output;
82+
// Divide by the new base again
83+
decimalValue /= b2;
84+
}
85+
return output;
86+
}
87+
}

0 commit comments

Comments
 (0)