In Java, Integer.toHexString(n) returns the value of n in base 16 as a String. It uses the digits 0-9 and the lowercase letters a-f, and it treats the number as an unsigned 32-bit value.
There is no 0x prefix and no minus sign. A negative int comes out as eight hex digits, such as ffffffff for -1.
A static method of java.lang.Integer: call it on the class, as Integer.toHexString(...). Integer lives in java.lang, so no import is needed.
Syntax
One argument: the int to show.
Java
String a = Integer.toHexString(255); // "ff"
String b = Integer.toHexString(48879); // "beef"
String c = Integer.toHexString(-1); // "ffffffff"
Call
What it does
Integer.toHexString(n)
Lowercase hex text of the unsigned 32-bit value.
Integer.toHexString(n).toUpperCase()
The same digits in capital letters.
String.format("%02x", n)
Hex padded to at least two digits.
Integer.parseInt(hex, 16)
The reverse: reads hex text back into an int.
No prefix is added, so write "0x" + Integer.toHexString(n) if you need one. Leading zeros are not added either.
Library declarations
Java
// java.lang.Integer
public static String toHexString(int i)
Parameters
Parameter
Description
i (int)
The number to show. Negative values are read as unsigned 32-bit numbers.
Return Value
Return Value
Description
String
The hex digits, 1 to 8 characters, in lowercase and without a prefix or sign.
All ones. -1 has every one of its 32 bits set, which is eight f digits.
-256 ends in 00. It is the bits of 255 flipped, which leaves ffffff00.
The limits.MAX_VALUE is 7fffffff, the largest value with the top bit clear, and MIN_VALUE is 80000000, just the top bit.
Signed text differs.toString(-255, 16) writes a minus sign and ff instead of the bit pattern.
Example 3 — Show Bytes as Two Hex Digits
Convert bytes to hex the wrong way and the right way, and spot the sign-extension trap.
Java
public class IntegerToHexBytes {
public static void main(String[] args) {
byte[] data = {10, -1, 127, -128};
for (byte b : data) {
String wrong = Integer.toHexString(b);
String right = Integer.toHexString(b & 0xFF);
if (right.length() < 2) {
right = "0" + right;
}
System.out.println(b + ": " + wrong + " vs " + right);
}
}
}
Output
10: a vs 0a
-1: ffffffff vs ff
127: 7f vs 7f
-128: ffffff80 vs 80
How It Works
A byte widens to int. The call receives an int, and Java copies the sign bit into the new upper bits.
The wrong column. A negative byte such as -1 becomes ffffffff, which is too long for a byte.
The mask fixes it.b & 0xFF keeps only the low 8 bits, so -1 gives ff and -128 gives 80.
Padding. The value 10 gives the single digit a, so the code adds a leading zero to make 0a.
Frequently Asked Questions
Call Integer.toHexString(n). For example, Integer.toHexString(255) returns "ff".
Call toUpperCase() on the result, or format the number with String.format("%X", n).
toHexString adds neither. Use String.format("%02x", n) for padding, or write "0x" + Integer.toHexString(n).
The byte is widened to an int with its sign. Use Integer.toHexString(b & 0xFF) to keep only the low 8 bits.
Use Integer.parseInt(text, 16), or Integer.decode("0x..") when the text has a prefix. For eight digits that fill all 32 bits, use Integer.parseUnsignedInt(text, 16).