Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

Integer.toHexString — hexadecimal representation

Integer.toHexString represents an int as an unsigned hexadecimal String, using lowercase letters without unnecessary leading zeros.

Version: Java SE 21; repository runtime, production runtime not attested

Syntax

public static String toHexString(int i)

The static method Integer.toHexString(int i) returns a String using digits 0–9 and a–f. It adds neither a 0x prefix nor zero padding; zero produces "0".

A negative argument is interpreted as unsigned by mathematically adding 2^32. Thus, Integer.toHexString(-1) produces "ffffffff", not "-1". In contrast, Integer.toString(-1, 16) preserves the minus sign.

Recover the original int with Integer.parseUnsignedInt(s, 16), including for a negative argument. This description targets Java SE 21; the production runtime is not attested.

Examples

public class Main {
    public static void main(String[] args) {
        System.out.println(Integer.toHexString(255));
        System.out.println(Integer.toHexString(0));
        System.out.println(Integer.toHexString(-1));
        System.out.println(Integer.toString(-1, 16));
        System.out.println(Integer.parseUnsignedInt("ffffffff", 16));
    }
}

Result

ff
0
ffffffff
-1
-1

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources