Java Integer toHexString() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Integer

Definition

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"
CallWhat 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

ParameterDescription
i (int)The number to show. Negative values are read as unsigned 32-bit numbers.

Return Value

Return ValueDescription
StringThe hex digits, 1 to 8 characters, in lowercase and without a prefix or sign.

Compare with Similar Methods

Related conversions:

MethodHow it differs
toBinaryString(n)The same bits in base 2: longer and easier to read bit by bit.
toOctalString(n)The same bits in base 8.
toString(n, 16)Signed hex text, such as "-ff" for -255.
decode("0x..")The reverse when the hex text carries a prefix.

Java Examples

Example 1 — Convert Everyday Numbers to Hex

Print a few numbers in hex, in lowercase and in capital letters.

Java
public class IntegerToHexDemo {
    public static void main(String[] args) {
        int[] numbers = {0, 9, 10, 255, 4096, 48879};
        for (int n : numbers) {
            String hex = Integer.toHexString(n);
            System.out.println(n + " -> " + hex + " -> " + hex.toUpperCase());
        }
    }
}

How It Works

  1. Digits and letters. 9 stays 9, but 10 becomes the letter a, because hex needs sixteen symbols.
  2. 255 is the largest byte. Two hex digits cover 0 to 255, so 255 becomes ff.
  3. A famous number. 48879 is beef in hex, since 190 × 256 + 239 equals 48879.
  4. Capitals on request. toUpperCase() changes the letters to BEEF without touching the digits.

Example 2 — See Negative Numbers as Eight Hex Digits

Print negative numbers and the limits of int in hex.

Java
public class IntegerToHexNegative {
    public static void main(String[] args) {
        System.out.println("-1:        " + Integer.toHexString(-1));
        System.out.println("-256:      " + Integer.toHexString(-256));
        System.out.println("-255:      " + Integer.toHexString(-255));
        System.out.println("MAX_VALUE: " + Integer.toHexString(Integer.MAX_VALUE));
        System.out.println("MIN_VALUE: " + Integer.toHexString(Integer.MIN_VALUE));
        System.out.println("signed:    " + Integer.toString(-255, 16));
    }
}

How It Works

  1. All ones. -1 has every one of its 32 bits set, which is eight f digits.
  2. -256 ends in 00. It is the bits of 255 flipped, which leaves ffffff00.
  3. The limits. MAX_VALUE is 7fffffff, the largest value with the top bit clear, and MIN_VALUE is 80000000, just the top bit.
  4. 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);
        }
    }
}

How It Works

  1. A byte widens to int. The call receives an int, and Java copies the sign bit into the new upper bits.
  2. The wrong column. A negative byte such as -1 becomes ffffffff, which is too long for a byte.
  3. The mask fixes it. b & 0xFF keeps only the low 8 bits, so -1 gives ff and -128 gives 80.
  4. 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).

Next: toOctalString()

Show the bits of an int in octal (base 8).

toOctalString() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful