Java Integer toHexString() Method
Example
Write numbers in hexadecimal:
public class Main {
public static void main(String[] args) {
System.out.println(Integer.toHexString(10));
System.out.println(Integer.toHexString(255));
System.out.println(Integer.toHexString(4096));
}
}
Definition and Usage
The toHexString() method returns a string with the number written in hexadecimal (base 16, with the digits 0 to 9 and the letters a to f).
The letters are lowercase. For a negative number, the result has 8 digits, because it shows how Java stores the number. For example, -1 gives ffffffff.
Tip: Add toUpperCase() to get uppercase letters: Integer.toHexString(255).toUpperCase() returns "FF".
Syntax
public static String toHexString(int i)
Parameter Values
| Parameter | Description |
|---|---|
| i | Required. The number to convert. |
Technical Details
| Returns: | A String with the number in hexadecimal. |
|---|
Related Pages
❮ Integer Methods