Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until the end of this week:

>> GET ACCESS NOW

November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until the end of this week:

>> GET ACCESS NOW

1. Overview

In this tutorial, we'll demonstrate how we can convert a String to a BigInteger. The BigInteger is commonly used for working with very large numerical values, which are usually the result of arbitrary arithmetic calculations.

2. Converting Decimal (Base 10) Integer Strings

To convert a decimal String to BigInteger, we'll use the BigInteger(String value) constructor:

String inputString = "878";
BigInteger result = new BigInteger(inputString);
assertEquals("878", result.toString());

3. Converting Non-Decimal Integer Strings

When using the default BigInteger(String value) constructor to convert a non-decimal String, like a hexadecimal number, we may get a NumberFormatException:

String inputString = "290f98";
new BigInteger(inputString);

This exception can be handled in two ways.

One way is to use the BigInteger(String value, int radix) constructor:

String inputString = "290f98";
BigInteger result = new BigInteger(inputString, 16);
assertEquals("2690968", result.toString());

In this case, we're specifying the radix, or base, as 16 for converting hexadecimal to decimal.

The other way is to first convert the non-decimal String into a byte array, and then use the BigIntenger(byte [] bytes) constructor:

byte[] inputStringBytes = inputString.getBytes();
BigInteger result = new BigInteger(inputStringBytes);
assertEquals("290f98", new String(result.toByteArray()));

This gives us the correct result because the BigIntenger(byte [] bytes) constructor turns a byte array containing the two's-complement binary representation into a BigInteger.

4. Conclusion

In this article, we looked at a few ways to convert a String to BigIntger in Java.

As usual, all code samples used in this tutorial are available over on GitHub.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until the end of this week:

>> GET ACCESS NOW

Generic footer banner
Comments are closed on this article!