Convert a hex string to an integer in Java
This post will discuss how to convert a hex string to an integer in Java.
1. Using Integer class
The Integer class provides several utility functions to convert a hex string to an integer in Java. You may use the Integer.decode() method to decode a String into an Integer. It accepts a hexadecimal number preceded with the radix specifier 0x or 0X, otherwise a NumberFormatException will be thrown.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
public class Main { public static void main(String[] args) { // Input hex string with radix specifier String hex = "0x7FFFFFFF"; int decimal = Integer.decode(hex); System.out.println(decimal); // 2147483647 } } |
Alternatively, you can use the Integer.parseInt() method to parse a string as a signed integer in the specified radix. Following is a simple example demonstrating usage of this:
|
1 2 3 4 5 6 7 8 9 10 11 |
public class Main { public static void main(String[] args) { String hex = "7FFFFFFF"; int decimal = Integer.parseInt(hex, 16); System.out.println(decimal); // 2147483647 } } |
The Integer.valueOf() method is a wrapper over the Integer.parseInt() method, but it returns an Integer instead of a primitive int.
|
1 2 3 4 5 6 7 8 9 10 11 |
public class Main { public static void main(String[] args) { String hex = "7FFFFFFF"; Integer decimal = Integer.valueOf(hex, 16); System.out.println(decimal); // 2147483647 } } |
2. Using Long class
All the above methods in Integer class fails for numbers greater than Integer.MAX_VALUE. That means it cannot parse a hexadecimal value greater than 0x7FFFFFFF. You should use Long.decode() method instead that can handle large numbers up to Long.MAX_VALUE (i.e., 0x7FFFFFFFFFFFFFFF in hexadecimal).
|
1 2 3 4 5 6 7 8 9 10 11 12 |
public class Main { public static void main(String[] args) { // Input hex string with radix specifier String hex = "0x7FFFFFFFFFFFFFF"; long decimal = Long.decode(hex); System.out.println(decimal); // 576460752303423487 } } |
As with the Integer class, you may use Long.parseLong() or Long.valueOf() method:
|
1 2 3 4 5 6 7 8 9 10 11 |
public class Main { public static void main(String[] args) { String hex = "7FFFFFFFFFFFFFF"; long decimal = Long.parseLong(hex, 16); System.out.println(decimal); // 576460752303423487 } } |
3. Using BigInteger class
For even bigger numbers than 0x7FFFFFFFFFFFFFFF (Long.MAX_VALUE), you may use the BigInteger class. The BigInteger constructor translates the specified value in the specified radix into a BigInteger.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import java.math.BigInteger; public class Main { public static void main(String[] args) { String hex = "7FFFFFFFFFFFFFFFFFFFFF"; BigInteger decimal = new BigInteger(hex, 16); System.out.println(decimal); // 154742504910672534362390527 } } |
That’s all about converting a hex string to an integer in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)