Convert int to hex string in Java
This post will discuss how to convert an integer to a hex string in Java.
1. Using Integer.toHexString() method
A simple solution to convert an integer to a hex string is using the Integer.toHexString() method. It returns the string representation of the specified integer as an “unsigned” integer in base 16.
|
1 2 3 4 5 6 7 8 9 10 |
public class Main { public static void main(String[] args) { int intValue = 2024; String hex = Integer.toHexString(intValue); System.out.println(hex); // 7e8 } } |
If uppercase letters are desired, you may call the String#toUpperCase() method on the result. Alternatively, you can use the toString() method of the Integer class which is overloaded to take a radix, and converts an integer to a string in the specified radix.
|
1 2 3 4 5 6 7 8 9 10 11 |
public class Main { public static void main(String[] args) { int intValue = 2024; String hex = Integer.toString(intValue, 16); System.out.println(hex); // 7e8 System.out.println(hex.toUpperCase()); // 7E8 } } |
2. Using Long.toHexString() method
Both above methods fail for numbers greater than Integer.MAX_VALUE. That means it cannot produce a hexadecimal value greater than 0x7FFFFFFF. You can use Long.toHexString() method instead that works for numbers less than equal to Long.MAX_VALUE (i.e., 0x7FFFFFFFFFFFFFFF in hexadecimal).
|
1 2 3 4 5 6 7 8 9 10 |
public class Main { public static void main(String[] args) { long longValue = Long.MAX_VALUE; String hex = Long.toHexString(longValue); System.out.println(hex); // 7fffffffffffffff } } |
Alternatively, you can use the Long.toString() method, which converts a long value to a string in the provided radix.
|
1 2 3 4 5 6 7 8 9 10 |
public class Main { public static void main(String[] args) { long longValue = Long.MAX_VALUE; String hex = Long.toString(longValue, 16); System.out.println(hex); // 7fffffffffffffff } } |
3. Using String.format() method
Finally, you can use the String.format() method that returns a formatted string using the specified format string and arguments. To format the argument as a hexadecimal integer, use the format string x.
|
1 2 3 4 5 6 7 8 9 10 |
public class Main { public static void main(String[] args) { int intValue = Integer.MAX_VALUE; String hex = String.format("%x", intValue); System.out.println(hex); // 7fffffff } } |
To convert the hexadecimal value to upper-case, replace x with X.
|
1 2 3 4 5 6 7 8 9 10 |
public class Main { public static void main(String[] args) { int intValue = Integer.MAX_VALUE; String hex = String.format("%X", intValue); System.out.println(hex); // 7FFFFFFF } } |
To prepend the hexadecimal string with the radix indicator (0x or 0X), use the # flag.
|
1 2 3 4 5 6 7 8 9 10 |
public class Main { public static void main(String[] args) { long longValue = Long.MAX_VALUE; String hex = String.format("%#X", longValue); System.out.println(hex); // 0X7FFFFFFFFFFFFFFF } } |
That’s all about converting an integer to a hex String 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 :)