2

In this program I need to be able to take a String and convert it into a base from (2-36) by using BigInteger

I'm new to java and I'm trying to find a way to use base conversion by using BigInteger instead of the method below. Although this works as intended, I'd like some tips on how to simplify it. For this project I'm unable to use the built-in base conversion functions in Integer or BigInteger.

Here is my conversion below.

  public static String convertInteger(String theValue, int initialBase, int finalBase) {
       
      BigInteger bigInteger = new BigInteger(theValue,initialBase);
           String value = bigInteger.toString(finalBase);
           value = value.toUpperCase();
           
       
       
       
       return value;
   }
}
Marshme
  • 21
  • 4
  • Your code doesn't use BigInteger at all. I suggest that you make a serious attempt to use BigInteger. If you can't figure it out, show us the attempt and we can give you some hints on how to proceed. (Hint: It strikes me that you can do this by taking characters from the string one at a time, converting them to numeric digits, and then doing this: number <- number * base + digit. Implement unsigned numbers first, then deal with signed numbers.) – Stephen C Aug 31 '20 at 00:49
  • I've updated my code, but I believe I can go about it a better way. You're version sounds better, although I'm unsure how to go about doing it. – Marshme Aug 31 '20 at 01:05
  • 3
    1) So what is wrong with the (latest) code you showed us? 2) Just give it a go. You don't need to be sure. 3) I suggest reading: [How do I ask and answer homework questions?](https://meta.stackoverflow.com/q/334822) – Stephen C Aug 31 '20 at 01:06
  • 1
    Your code seems to be perfectly fine. To simplify it, just combine it into a single statement: `return new BigInteger(theValue, initialBase).toString(finalBase).toUpperCase();` – Andreas Aug 31 '20 at 01:48

0 Answers0