NewbieLearner
NewbieLearner

Reputation: 95

Need an alternative for .parse function

I am currently dealing with numbers with different bases. I am using the function Long.parseLong to convert a number from base int to its decimal value:

Long.parseLong("A", 16) to convert A which is a hexadecimal to its decimal value 10. I then use Long.toString(10, 16) to convert it back to A.

The problem now is that one of the entries in the problem set is "AAAAAAAAAAAAAAAAAAA" and when I use the Long.parseLong(); function, it returns an error because the size of the conversion can't fit to a Long. I tried to use Float, but I'm afraid that there is no Float.parseFloat(String, int) with the int as its radix.

Is there an alternative which I can use? BigInteger also doesn't have any parse function with the radix.

Any help will be much appreciated.

Upvotes: 2

Views: 378

Answers (1)

BigInteger has a constructor, which does radix conversion for you:

BigInteger num = new BigInteger("AAAAAAAAAAAAAAAAAAA", 16);

If you wish to convert back to hexadecimal, you can use toString:

String hex = num.toString(16);

Upvotes: 7

Related Questions