I had convert() as a static method before, but I've changed it prior to uploading. The computer I'm on doesn't have any IDE and I can't be bothered compiling and running through a CLI, so I hope it works (it seems like it should :P).
It's a simple Base Convert for Java, you create a new BaseConverter object with parameters inBase - the base the strings you want to convert are in - and outBase - the base you want the strings to be converted to - (they can be changed with the set methods). You then call the method convert(String numString) with numString being the number you want to convert (must be in base-inBase). There are also get methods to obtain the current in and out bases.
Comments are welcome.
import java.util.ArrayList;
public class BaseConverter {
private int inBase;
private int outBase;
public BaseConverter(int iB, int oB) {
inBase = iB;
outBase = oB;
}
public int getInBase() {
return inBase;
}
public int getOutBase() {
return outBase;
}
public void setInBase(int iB) {
inBase = iB;
}
public void setOutBase(int oB) {
outBase = oB;
}
public String convert(String numString) {
String answer = "";
int[] numReversed = new int[numString.length()];
int decimalValue = 0;
int arrayIndex = 0;
for (int currentCharIndex = numString.length() - 1; currentCharIndex >= 0; currentCharIndex--) {
switch (numString.charAt(currentCharIndex)) {
case 'a':
case 'A': numReversed[arrayIndex] = ((int) 'A') - 55; break;
case 'b':
case 'B': numReversed[arrayIndex] = ((int) 'B') - 55; break;
case 'c':
case 'C': numReversed[arrayIndex] = ((int) 'C') - 55; break;
case 'd':
case 'D': numReversed[arrayIndex] = ((int) 'D') - 55; break;
case 'e':
case 'E': numReversed[arrayIndex] = ((int) 'E') - 55; break;
case 'f':
case 'F': numReversed[arrayIndex] = ((int) 'F') - 55; break;
default: numReversed[arrayIndex] = Integer.parseInt(Character.toString(numString.charAt(currentCharIndex)));
}
if (numReversed[arrayIndex] > inBase - 1) {
return numString + " is not a valid base-" + inBase + " number.";
}
numReversed[arrayIndex] *= Math.pow(inBase,arrayIndex);
arrayIndex++;
}
for (int b = 0; b < numReversed.length; b++)
decimalValue += numReversed[b];
if (outBase == 10)
return Integer.toString(decimalValue);
ArrayList<Integer> nums = new ArrayList<Integer>();
while (decimalValue != 0) {
nums.add(decimalValue % outBase);
decimalValue /= outBase;
}
for (int currentArrayIndex = nums.size() - 1; currentArrayIndex >= 0; currentArrayIndex--) {
if (nums.get(currentArrayIndex) >= 10) {
answer += Character.toString((char) (nums.get(currentArrayIndex) + 55));
continue;
}
answer += Integer.toString(nums.get(currentArrayIndex));
}
return answer;
}
}
You should theoretically allow bases beyond a radix of 16. I see your switch only goes to F/f, when it should go to Z/z. To make it easier on yourself, check whether it's a letter and uppercase and convert to lowercase if so. Then do the standard equation you are using to convert it to it's decimal representation.
My two cents.
Nice to see some new Java code flowing in. Although, I don't see a reason to break up data in, out, and retrieval so profusely. It only complicates a straight forward snippet more than should be so. Looks good though, and it compiles just fine. However, your description of the snippet doesn't accurately depict what the snippet does, at least that's how I understood it.